
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
	<head><meta forua="true" http-equiv="Cache-Control" content="max-age=0" /></head><card id="MainCard" title="&#x6B22;&#x8FCE;&#x5149;&#x4E34;"><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a><br/>&nbsp;</p><p><b>&#x6807;&#x9898;&#x3A;</b> &#x45;&#x66;&#x66;&#x65;&#x63;&#x74;&#x69;&#x76;&#x65;&#x20;&#x43;&#x23;&#x20;&#x539F;&#x5219;&#x32;&#x36;&#xFF1A;&#x7528;&#x49;&#x43;&#x6F;&#x6D;&#x70;&#x61;&#x72;&#x61;&#x62;&#x6C;&#x65;&#x548C;&#x49;&#x43;&#x6F;&#x6D;&#x70;&#x61;&#x72;&#x65;&#x72;&#x5B9E;&#x73B0;&#x5BF9;&#x8C61;&#x7684;&#x987A;&#x5E8F;&#x5173;&#x7CFB;</p><p><b>&#x4F5C;&#x8005;&#x3A;</b> &#x64;&#x6E;&#x61;&#x77;&#x6F;</p><p><b>&#x65E5;&#x671F;&#x3A;</b> &#x32;&#x30;&#x31;&#x30;&#x2D;&#x30;&#x31;&#x2D;&#x30;&#x31;&#x20;&#x31;&#x30;&#x3A;&#x30;&#x37;&#x20;&#x41;&#x4D;</p><p><b>&#x5206;&#x7C7B;&#x3A;</b> <a href="wap.asp?do=showLog&amp;cateID=5">&#x57;&#x65;&#x62;&#x7F16;&#x7A0B;</a></p><p><b>&#x5185;&#x5BB9;&#x3A;</b> &#x4F60;&#x7684;&#x7C7B;&#x578B;&#x5E94;&#x8BE5;&#x6709;&#x4E00;&#x4E2A;&#x987A;&#x5E8F;&#x5173;&#x7CFB;&#xFF0C;&#x4EE5;&#x4FBF;&#x5728;&#x96C6;&#x5408;&#x4E2D;&#x63CF;&#x8FF0;&#x5B83;&#x4EEC;&#x5982;&#x4F55;&#x5B58;&#x50A8;&#x4EE5;&#x53CA;&#x6392;&#x5E8F;&#x3002;.Net&#x6846;&#x67B6;&#x4E3A;&#x4F60;&#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x4E2A;&#x63A5;&#x53E3;&#x6765;&#x63CF;&#x8FF0;&#x5BF9;&#x8C61;&#x7684;&#x987A;&#x5E8F;&#x5173;&#x7CFB;&#xFF1A;IComparable &#x548C;IComparer&#x3002;IComparable &#x4E3A;&#x4F60;&#x7684;&#x7C7B;&#x5B9A;&#x4E49;&#x4E86;&#x81EA;&#x7136;&#x987A;&#x5E8F;&#xFF0C;&#x800C;&#x5B9E;&#x73B0;IComparer&#x63A5;&#x53E3;&#x7684;&#x7C7B;&#x53EF;&#x4EE5;&#x63CF;&#x8FF0;&#x5176;&#x5B83;&#x53EF;&#x9009;&#x7684;&#x987A;&#x5E8F;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x65F6;&#xFF0C;&#x5B9A;&#x4E49;&#x5E76;&#x5B9E;&#x73B0;&#x4F60;&#x81EA;&#x5DF1;&#x5173;&#x7CFB;&#x64CD;&#x4F5C;&#x7B26;(&lt;&#xFF0C;&gt;&#xFF0C;&lt;=&#xFF0C;&gt;=)&#xFF0C;&#x7528;&#x4E8E;&#x907F;&#x514D;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x9ED8;&#x8BA4;&#x6BD4;&#x8F83;&#x5173;&#x7CFB;&#x7684;&#x4F4E;&#x6548;&#x95EE;&#x9898;&#x3002;&#x8FD9;&#x4E00;&#x539F;&#x5219;&#x5C06;&#x8BA8;&#x8BBA;&#x5982;&#x4F55;&#x5B9E;&#x73B0;&#x987A;&#x5E8F;&#x5173;&#x7CFB;&#xFF0C;&#x4EE5;&#x4FBF;.Net&#x6846;&#x67B6;&#x7684;&#x6838;&#x5FC3;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F60;&#x5B9A;&#x4E49;&#x7684;&#x63A5;&#x53E3;&#x5BF9;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x3002;&#x8FD9;&#x6837;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x5728;&#x4E9B;&#x64CD;&#x4F5C;&#x4E0A;&#x5F97;&#x66F4;&#x597D;&#x7684;&#x6548;&#x7387;&#x3002;<br/>IComparable&#x63A5;&#x53E3;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF1A;CompareTo()&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x6CBF;&#x7528;&#x4E86;&#x4F20;&#x7EDF;&#x7684;C&#x51FD;&#x6570;&#x5E93;&#x91CC;&#x7684;strcmp&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x73B0;&#x539F;&#x5219;&#xFF1A;&#x5982;&#x679C;&#x5F53;&#x524D;&#x5BF9;&#x8C61;&#x6BD4;&#x76EE;&#x6807;&#x5BF9;&#x8C61;&#x5C0F;&#xFF0C;&#x5B83;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x5C0F;&#x4E8E;0&#xFF1B;&#x5982;&#x679C;&#x76F8;&#x7B49;&#x5C31;&#x8FD4;&#x56DE;0&#xFF1B;&#x5982;&#x679C;&#x5F53;&#x524D;&#x5BF9;&#x8C61;&#x6BD4;&#x76EE;&#x6807;&#x5BF9;&#x8C61;&#x5927;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x5C31;&#x5927;&#x4E8E;0&#x3002;IComparable&#x4EE5;System.Object&#x505A;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x56E0;&#x6B64;&#x5728;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x4F60;&#x987B;&#x8981;&#x5BF9;&#x8FD0;&#x884C;&#x65F6;&#x7684;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x68C0;&#x6D4B;&#x3002;&#x6BCF;&#x6B21;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;&#x65F6;&#xFF0C;&#x4F60;&#x5FC5;&#x987B;&#x91CD;&#x65B0;&#x89E3;&#x91CA;&#x53C2;&#x6570;&#x7684;&#x7C7B;&#x578B;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct Customer : IComparable<br/>{<br/>&nbsp;&nbsp;private readonly string _name;<br/>&nbsp;&nbsp;public Customer( string name )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_name = name;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#region IComparable Members<br/>&nbsp;&nbsp;public int CompareTo( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( ! ( right is Customer ) )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new ArgumentException( &#34;Argument not a customer&#34;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;right&#34; );<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer rightCustomer = ( Customer )right;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return _name.CompareTo( rightCustomer._name );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#endregion<br/>}<br/>&#x5173;&#x4E8E;&#x5B9E;&#x73B0;&#x6BD4;&#x8F83;&#x4E0E;IComparable&#x63A5;&#x53E3;&#x7684;&#x4E00;&#x81F4;&#x6027;&#x6709;&#x5F88;&#x591A;&#x4E0D;&#x592A;&#x559C;&#x6B22;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x9996;&#x5148;&#x5C31;&#x662F;&#x4F60;&#x8981;&#x68C0;&#x6D4B;&#x53C2;&#x6570;&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x7C7B;&#x578B;&#x3002;&#x4E0D;&#x6B63;&#x786E;&#x7684;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x7528;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x505A;&#x4E3A;&#x53C2;&#x6570;&#x6765;&#x8C03;&#x7528;CompareTo&#x65B9;&#x6CD5;&#x3002;&#x8FD8;&#x6709;&#xFF0C;&#x6B63;&#x786E;&#x7684;&#x53C2;&#x6570;&#x8FD8;&#x5FC5;&#x987B;&#x8FDB;&#x884C;&#x88C5;&#x7BB1;&#x4E0E;&#x62C6;&#x7BB1;&#x540E;&#x624D;&#x80FD;&#x63D0;&#x4F9B;&#x5B9E;&#x9645;&#x7684;&#x6BD4;&#x8F83;&#x3002;&#x6BCF;&#x6B21;&#x6BD4;&#x8F83;&#x90FD;&#x8981;&#x8FDB;&#x884C;&#x8FD9;&#x6837;&#x989D;&#x5916;&#x7684;&#x5F00;&#x9500;&#x3002;&#x5728;&#x5BF9;&#x96C6;&#x5408;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x65F6;&#xFF0C;&#x5728;&#x5BF9;&#x8C61;&#x4E0A;&#x8FDB;&#x884C;&#x7684;&#x5E73;&#x5747;&#x6BD4;&#x8F83;&#x6B21;&#x6570;&#x4E3A;N x log(N)&#xFF0C;&#x800C;&#x6BCF;&#x6B21;&#x90FD;&#x4F1A;&#x4EA7;&#x751F;&#x4E09;&#x6B21;&#x88C5;&#x7BB1;&#x4E0E;&#x62C6;&#x7BB1;&#x3002;&#x5BF9;&#x4E8E;&#x4E00;&#x4E2A;&#x6709;1000&#x4E2A;&#x70B9;&#x7684;&#x6570;&#x7EC4;&#x6765;&#x8BF4;&#xFF0C;&#x8FD9;&#x5C06;&#x4F1A;&#x4EA7;&#x751F;&#x5927;&#x6982;20000&#x6B21;&#x7684;&#x88C5;&#x7BB1;&#x4E0E;&#x62C6;&#x7BB1;&#x64CD;&#x4F5C;&#xFF0C;&#x5E73;&#x5747;&#x8BA1;&#x7B97;&#xFF1A;N x log(n) &#x6709;7000&#x6B21;&#xFF0C;&#x6BCF;&#x6B21;&#x6BD4;&#x8F83;&#x6709;3&#x6B21;&#x88C5;&#x7BB1;&#x4E0E;&#x62C6;&#x7BB1;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x4F60;&#x5FC5;&#x987B;&#x81EA;&#x5DF1;&#x627E;&#x4E2A;&#x53EF;&#x9009;&#x7684;&#x6BD4;&#x8F83;&#x65B9;&#x6CD5;&#x3002;&#x4F60;&#x65E0;&#x6CD5;&#x6539;&#x53D8;IComparable.CompareTo()&#x7684;&#x5B9A;&#x4E49;&#xFF0C;&#x4F46;&#x8FD9;&#x5E76;&#x4E0D;&#x610F;&#x5473;&#x7740;&#x4F60;&#x8981;&#x88AB;&#x8FEB;&#x8BA9;&#x4F60;&#x7684;&#x7528;&#x6237;&#x5728;&#x4E00;&#x4E2A;&#x5F31;&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x73B0;&#x4E0A;&#x4E5F;&#x8981;&#x5FCD;&#x53D7;&#x6027;&#x80FD;&#x7684;&#x635F;&#x5931;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x91CD;&#x8F7D;CompareTo()&#x65B9;&#x6CD5;&#xFF0C;&#x8BA9;&#x5B83;&#x53EA;&#x5BF9;Customer &#x5BF9;&#x8C61;&#x64CD;&#x4F5C;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct Customer : IComparable<br/>{<br/>&nbsp;&nbsp;private string _name;<br/>&nbsp;&nbsp;public Customer( string name )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_name = name;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#region IComparable Members<br/>&nbsp;&nbsp;// IComparable.CompareTo()<br/>&nbsp;&nbsp;// This is not type safe. The runtime type<br/>&nbsp;&nbsp;// of the right parameter must be checked.<br/>&nbsp;&nbsp;int IComparable.CompareTo( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( ! ( right is Customer ) )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new ArgumentException( &#34;Argument not a customer&#34;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;right&#34; );<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer rightCustomer = ( Customer )right;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return CompareTo( rightCustomer );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;// type-safe CompareTo.<br/>&nbsp;&nbsp;// Right is a customer, o&#114; derived from Customer.<br/>&nbsp;&nbsp;public int CompareTo( Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return _name.CompareTo( right._name );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#endregion<br/>}<br/>&#x73B0;&#x5728;&#xFF0C;IComparable.CompareTo()&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x9690;&#x5F0F;&#x7684;&#x63A5;&#x53E3;&#x5B9E;&#x73B0;&#xFF0C;&#x5B83;&#x53EA;&#x80FD;&#x901A;&#x8FC7;IComparable &#x63A5;&#x53E3;&#x7684;&#x5F15;&#x7528;&#x624D;&#x80FD;&#x8C03;&#x7528;&#x3002;&#x4F60;&#x7684;&#x7528;&#x6237;&#x5219;&#x53EA;&#x80FD;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x5B89;&#x5168;&#x7684;&#x8C03;&#x7528;&#xFF0C;&#x800C;&#x4E14;&#x4E0D;&#x5B89;&#x5168;&#x7684;&#x6BD4;&#x8F83;&#x662F;&#x4E0D;&#x53EF;&#x80FD;&#x8BBF;&#x95EE;&#x7684;&#x3002;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x65E0;&#x610F;&#x7684;&#x9519;&#x8BEF;&#x5C31;&#x4E0D;&#x80FD;&#x901A;&#x8FC7;&#x7F16;&#x8BD1;&#x4E86;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Customer c1;<br/>Employee e1;<br/>if ( c1.CompareTo( e1 )&nbsp;&nbsp;&gt;&nbsp;&nbsp;0 )<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Customer one is greater&#34; );<br/>&#x8FD9;&#x4E0D;&#x80FD;&#x901A;&#x8FC7;&#x7F16;&#x8BD1;&#xFF0C;&#x56E0;&#x4E3A;&#x5BF9;&#x4E8E;&#x516C;&#x5171;&#x7684;Customer.CompareTo(Customer right)&#x65B9;&#x6CD5;&#x5728;&#x53C2;&#x6570;&#x4E0A;&#x4E0D;&#x5339;&#x914D;&#xFF0C;&#x800C;IComparable. CompareTo(object right)&#x65B9;&#x6CD5;&#x53C8;&#x4E0D;&#x53EF;&#x8BBF;&#x95EE;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x4F60;&#x53EA;&#x80FD;&#x901A;&#x8FC7;&#x5F3A;&#x5236;&#x8F6C;&#x5316;&#x4E3A;IComparable &#x63A5;&#x53E3;&#x540E;&#x624D;&#x80FD;&#x8BBF;&#x95EE;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Customer c1;<br/>Employee e1;<br/>if ( ( c1 as IComparable ).CompareTo( e1 )&nbsp;&nbsp;&gt;&nbsp;&nbsp;0 )<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Customer one is greater&#34; );<br/>&#x5F53;&#x4F60;&#x901A;&#x8FC7;&#x9690;&#x5F0F;&#x5B9E;&#x73B0;IComparable&#x63A5;&#x53E3;&#x800C;&#x53C8;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x5B89;&#x5168;&#x7684;&#x6BD4;&#x8F83;&#x65F6;&#xFF0C;&#x91CD;&#x8F7D;&#x7248;&#x672C;&#x7684;&#x5F3A;&#x7C7B;&#x578B;&#x6BD4;&#x8F83;&#x589E;&#x52A0;&#x4E86;&#x6027;&#x80FD;&#xFF0C;&#x800C;&#x4E14;&#x51CF;&#x5C11;&#x4E86;&#x5176;&#x4ED6;&#x4EBA;&#x8BEF;&#x7528;CompareTo&#x65B9;&#x6CD5;&#x7684;&#x53EF;&#x80FD;&#x3002;&#x4F60;&#x8FD8;&#x4E0D;&#x80FD;&#x770B;&#x5230;.Net&#x6846;&#x67B6;&#x91CC;Sort&#x51FD;&#x6570;&#x7684;&#x6240;&#x6709;&#x597D;&#x5904;&#xFF0C;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x5B83;&#x8FD8;&#x662F;&#x7528;&#x63A5;&#x53E3;&#x6307;&#x9488;(&#x53C2;&#x89C1;&#x539F;&#x5219;19)&#x6765;&#x8BBF;&#x95EE;CompareTo()&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x5728;&#x5DF2;&#x9053;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x4EE3;&#x7801;&#x7684;&#x6027;&#x80FD;&#x4F1A;&#x597D;&#x4E00;&#x4E9B;&#x3002;<br/>&#x6211;&#x4EEC;&#x518D;&#x5BF9;Customer &#x7ED3;&#x6784;&#x505A;&#x4E00;&#x4E2A;&#x5C0F;&#x7684;&#x4FEE;&#x6539;&#xFF0C;C#&#x8BED;&#x8A00;&#x53EF;&#x4EE5;&#x91CD;&#x8F7D;&#x6807;&#x51C6;&#x7684;&#x5173;&#x7CFB;&#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x8FD9;&#x4E9B;&#x5E94;&#x8BE5;&#x5229;&#x7528;&#x7C7B;&#x578B;&#x5B89;&#x5168;&#x7684;CompareTo()&#x65B9;&#x6CD5;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct Customer : IComparable<br/>{<br/>&nbsp;&nbsp;private string _name;<br/>&nbsp;&nbsp;public Customer( string name )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_name = name;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#region IComparable Members<br/>&nbsp;&nbsp;// IComparable.CompareTo()<br/>&nbsp;&nbsp;// This is not type safe. The runtime type<br/>&nbsp;&nbsp;// of the right parameter must be checked.<br/>&nbsp;&nbsp;int IComparable.CompareTo( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( ! ( right is Customer ) )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new ArgumentException( &#34;Argument not a customer&#34;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;right&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer rightCustomer = ( Customer )right;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return CompareTo( rightCustomer );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;// type-safe CompareTo.<br/>&nbsp;&nbsp;// Right is a customer, o&#114; derived from Customer.<br/>&nbsp;&nbsp;public int CompareTo( Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return _name.CompareTo( right._name );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;// Relational Operators.<br/>&nbsp;&nbsp;public static bool operator &lt; ( Customer left,<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return left.CompareTo( right ) &lt; 0;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public static bool operator &lt;=( Customer left,<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return left.CompareTo( right ) &lt;= 0;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public static bool operator &gt;( Customer left,<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return left.CompareTo( right ) &gt; 0;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public static bool operator &gt;=( Customer left,<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return left.CompareTo( right ) &gt;= 0;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#endregion<br/>}<br/>&#x6240;&#x6709;&#x5BA2;&#x6237;&#x7684;&#x987A;&#x5E8F;&#x5173;&#x7CFB;&#x5C31;&#x662F;&#x8FD9;&#x6837;&#xFF1A;&#x4EE5;&#x540D;&#x5B57;&#x6392;&#x5E8F;&#x3002;&#x4E0D;&#x4E45;&#xFF0C;&#x4F60;&#x5F88;&#x53EF;&#x80FD;&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x62A5;&#x8868;&#xFF0C;&#x8981;&#x4EE5;&#x5BA2;&#x6237;&#x7684;&#x6536;&#x5165;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x3002;&#x4F60;&#x8FD8;&#x662F;&#x9700;&#x8981;Custom&#x7ED3;&#x6784;&#x91CC;&#x5B9A;&#x4E49;&#x7684;&#x666E;&#x901A;&#x7684;&#x6BD4;&#x8F83;&#x673A;&#x5236;&#xFF1A;&#x4EE5;&#x540D;&#x5B57;&#x6392;&#x5E8F;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5B9E;&#x73B0;&#x4E86;IComparer &#x63A5;&#x53E3;&#x7684;&#x7C7B;&#x6765;&#x5B8C;&#x6210;&#x8FD9;&#x4E2A;&#x65B0;&#x589E;&#x7684;&#x9700;&#x6C42;&#x3002;IComparer&#x7ED9;&#x7C7B;&#x578B;&#x6BD4;&#x8F83;&#x63D0;&#x4F9B;&#x53E6;&#x4E00;&#x4E2A;&#x6807;&#x51C6;&#x7684;&#x9009;&#x62E9;&#xFF0C;&#x5728;.Net FCL&#x4E2D;&#x4EFB;&#x4F55;&#x5728;IComparable&#x63A5;&#x53E3;&#x4E0A;&#x5DE5;&#x4F5C;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x90FD;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x91CD;&#x8F7D;&#xFF0C;&#x4EE5;&#x4FBF;&#x901A;&#x8FC7;&#x63A5;&#x53E3;&#x5BF9;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x3002;&#x56E0;&#x4E3A;&#x4F60;&#x662F;Customer&#x7ED3;&#x6784;&#x7684;&#x4F5C;&#x8005;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7C7B;(RevenueComparer)&#x505A;&#x4E3A;Customer&#x7ED3;&#x6784;&#x7684;&#x4E00;&#x4E2A;&#x79C1;&#x6709;&#x7684;&#x5D4C;&#x5957;&#x7C7B;&#x3002;&#x5B83;&#x901A;&#x8FC7;Customer&#x7ED3;&#x6784;&#x7684;&#x9759;&#x6001;&#x5C5E;&#x6027;&#x66B4;&#x9732;&#x7ED9;&#x7528;&#x6237;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct Customer : IComparable<br/>{<br/>&nbsp;&nbsp;private string _name;<br/>&nbsp;&nbsp;private double _revenue;<br/>&nbsp;&nbsp;// code from earlier example elided.<br/>&nbsp;&nbsp;private static RevenueComparer _revComp = null;<br/>&nbsp;&nbsp;// return an object that implements IComparer<br/>&nbsp;&nbsp;// use lazy evaluation to cr&#101;ate just one.<br/>&nbsp;&nbsp;public static IComparer RevenueCompare<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;get<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( _revComp == null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_revComp = new RevenueComparer();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return _revComp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;// Class to compare customers by revenue.<br/>&nbsp;&nbsp;// This is always used via the interface pointer,<br/>&nbsp;&nbsp;// so only provide the interface override.<br/>&nbsp;&nbsp;private class RevenueComparer : IComparer<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;#region IComparer Members<br/>&nbsp;&nbsp;&nbsp;&nbsp;int IComparer.Compare( object left, object right )<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( ! ( left is Customer ) )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new ArgumentException(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;Argument is not a Customer&#34;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;left&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (! ( right is Customer) )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new ArgumentException(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;Argument is not a Customer&#34;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;right&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer leftCustomer = ( Customer ) left;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer rightCustomer = ( Customer ) right;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return leftCustomer._revenue.CompareTo(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rightCustomer._revenue);<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;&nbsp;#endregion<br/>&nbsp;&nbsp;}<br/>}<br/>&#x6700;&#x540E;&#x8FD9;&#x4E2A;&#x7248;&#x672C;&#x7684;Customer&#x7ED3;&#x6784;&#xFF0C;&#x5305;&#x542B;&#x4E86;RevenueComparer&#x7C7B;&#xFF0C;&#x8FD9;&#x6837;&#x4F60;&#x5C31;&#x53EF;&#x4EE5;&#x4EE5;&#x81EA;&#x7136;&#x987A;&#x5E8F;-&#x540D;&#x5B57;&#xFF0C;&#x5BF9;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF1B;&#x8FD8;&#x53EF;&#x6709;&#x4E00;&#x4E2A;&#x9009;&#x62E9;&#x5C31;&#x662F;&#x7528;&#x8FD9;&#x4E2A;&#x66B4;&#x9732;&#x51FA;&#x6765;&#x7684;&#xFF0C;&#x5B9E;&#x73B0;&#x4E86;IComparer &#x63A5;&#x53E3;&#x7684;&#x7C7B;&#xFF0C;&#x4EE5;&#x6536;&#x5165;&#x5BF9;&#x5BA2;&#x6237;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x3002;&#x5982;&#x679C;&#x4F60;&#x6CA1;&#x6709;&#x529E;&#x6CD5;&#x8BBF;&#x95EE;Customer&#x7C7B;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#xFF0C;&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;IComparer&#x63A5;&#x53E3;&#xFF0C;&#x7528;&#x4E8E;&#x5BF9;&#x5B83;&#x7684;&#x4EFB;&#x4F55;&#x516C;&#x5171;&#x5C5E;&#x6027;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x3002;&#x53EA;&#x6709;&#x5728;&#x4F60;&#x65E0;&#x6CD5;&#x53D6;&#x5F97;&#x6E90;&#x4EE3;&#x7801;&#x65F6;&#x624D;&#x4F7F;&#x7528;&#x8FD9;&#x6837;&#x7684;&#x4E60;&#x60EF;&#xFF0C;&#x540C;&#x65F6;&#x4E5F;&#x662F;&#x5728;.Net&#x6846;&#x67B6;&#x91CC;&#x7684;&#x4E00;&#x4E2A;&#x7C7B;&#x987B;&#x8981;&#x4E0D;&#x540C;&#x7684;&#x6392;&#x5E8F;&#x4F9D;&#x636E;&#x65F6;&#x624D;&#x8FD9;&#x6837;&#x7528;&#x3002;<br/>&#x8FD9;&#x4E00;&#x539F;&#x5219;&#x91CC;&#x6CA1;&#x6709;&#x6D89;&#x53CA;Equals()&#x65B9;&#x6CD5;&#x548C;==&#x64CD;&#x4F5C;&#x7B26;(&#x53C2;&#x89C1;&#x539F;&#x5219;9)&#x3002;&#x6392;&#x5E8F;&#x548C;&#x76F8;&#x7B49;&#x662F;&#x5F88;&#x6E05;&#x695A;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x4F60;&#x4E0D;&#x7528;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x76F8;&#x7B49;&#x6BD4;&#x8F83;&#x6765;&#x8868;&#x8FBE;&#x6392;&#x5E8F;&#x5173;&#x7CFB;&#x3002; &#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x901A;&#x5E38;&#x662F;&#x57FA;&#x4E8E;&#x5BF9;&#x8C61;&#x7684;&#x5185;&#x5BB9;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x7684;&#xFF0C;&#x800C;&#x76F8;&#x7B49;&#x5219;&#x662F;&#x57FA;&#x4E8E;&#x5BF9;&#x8C61;&#x7684;ID&#x7684;&#x3002;&#x5728;Equals()&#x8FD4;&#x56DE;false&#x65F6;&#xFF0C;CompareTo()&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;0&#x3002;&#x8FD9;&#x5B8C;&#x5168;&#x662F;&#x5408;&#x6CD5;&#x7684;&#xFF0C;&#x76F8;&#x7B49;&#x4E0E;&#x6392;&#x5E8F;&#x5B8C;&#x5168;&#x6CA1;&#x5FC5;&#x8981;&#x4E00;&#x6837;&#x3002;<br/>(&#x8BD1;&#x6CE8;&#xFF1A;&#x6CE8;&#x610F;&#x4F5C;&#x8005;&#x8FD9;&#x91CC;&#x8BA8;&#x8BBA;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x662F;&#x6392;&#x5E8F;&#x4E0E;&#x76F8;&#x7B49;&#x8FD9;&#x4E24;&#x79CD;&#x64CD;&#x4F5C;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5177;&#x4F53;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x5BF9;&#x4E8E;&#x4E00;&#x4E9B;&#x7279;&#x6B8A;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x76F8;&#x7B49;&#x4E0E;&#x6392;&#x5E8F;&#x53EF;&#x80FD;&#x76F8;&#x5173;&#x3002;)<br/>IComparable &#x548C;IComparer&#x63A5;&#x53E3;&#x4E3A;&#x7C7B;&#x578B;&#x7684;&#x6392;&#x5E8F;&#x63D0;&#x4F9B;&#x4E86;&#x6807;&#x51C6;&#x7684;&#x673A;&#x5236;&#xFF0C;IComparable &#x5E94;&#x8BE5;&#x5728;&#x5927;&#x591A;&#x6570;&#x81EA;&#x7136;&#x6392;&#x5E8F;&#x4E0B;&#x4F7F;&#x7528;&#x3002;&#x5F53;&#x4F60;&#x5B9E;&#x73B0;IComparable&#x63A5;&#x53E3;&#x65F6;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x4E3A;&#x7C7B;&#x578B;&#x6392;&#x5E8F;&#x91CD;&#x8F7D;&#x4E00;&#x81F4;&#x7684;&#x6BD4;&#x8F83;&#x64CD;&#x4F5C;&#x7B26;(&lt;, &gt;, &lt;=, &gt;=)&#x3002;IComparable.CompareTo()&#x4F7F;&#x7528;&#x7684;&#x662F;System.Object&#x505A;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x540C;&#x6837;&#x4F60;&#x4E5F;&#x8981;&#x91CD;&#x8F7D;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x5B89;&#x5168;&#x7684;CompareTo()&#x65B9;&#x6CD5;&#x3002;IComparer &#x53EF;&#x4EE5;&#x4E3A;&#x6392;&#x5E8F;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7684;&#x6392;&#x5E8F;&#x4F9D;&#x636E;&#xFF0C;&#x8FD9;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x4E00;&#x4E9B;&#x6CA1;&#x6709;&#x7ED9;&#x4F60;&#x63D0;&#x4F9B;&#x6392;&#x5E8F;&#x4F9D;&#x636E;&#x7684;&#x7C7B;&#x578B;&#x4E0A;&#xFF0C;&#x63D0;&#x4F9B;&#x4F60;&#x81EA;&#x5DF1;&#x7684;&#x6392;&#x5E8F;&#x4F9D;&#x636E;&#x3002;<br/>========================================================&nbsp;&nbsp;&nbsp;&nbsp;<br/>Item 26: Implement o&#114;dering Relations with IComparable and IComparer<br/>Your types need o&#114;dering relationships to describe how collections should be sorted and searched. The .NET Framework defines two interfaces that describe o&#114;dering relationships in your types: IComparable and IComparer.IComparable defines the natural o&#114;der for your types. A type implements IComparer to describe alt&#101;rnative o&#114;derings. You can define your own implementations of the relational operators (&lt;, &gt;, &lt;=, &gt;=) to provide type-specific comparisons, to avoid some runtime inefficiencies in the interface implementations. This item discusses how to implement o&#114;dering relations so that the core .NET Framework o&#114;ders your types through the defined interfaces and so that other users get the best performance from these operations.<br/>The IComparable interface contains one method: CompareTo(). This method follows the long-standing tradition started with the C library function strcmp: Its return value is less than 0 if the current object is less than the comparison object, 0 if they are equal, and greater than 0 if the current object is greater than the comparison object. IComparable takes parameters of type System.Object. You need to perform runtime type checking on the argument to this function. Every time comparisons are performed, you must reinterpret the type of the argument:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct Customer : IComparable<br/>{<br/>&nbsp;&nbsp;private readonly string _name;<br/>&nbsp;&nbsp;public Customer( string name )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_name = name;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#region IComparable Members<br/>&nbsp;&nbsp;public int CompareTo( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( ! ( right is Customer ) )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new ArgumentException( &#34;Argument not a customer&#34;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;right&#34; );<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer rightCustomer = ( Customer )right;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return _name.CompareTo( rightCustomer._name );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#endregion<br/>}<br/>There&#39;s a lot to dislike about implementing comparisons consistent with the IComparable interface. You&#39;ve got to check the runtime type of the argument. Incorrect code could legally call this method with anything as the argument to the CompareTo method. More so, proper arguments must be boxed and unboxed to provide the actual comparison. That&#39;s an extra runtime expense for each compare. Sorting a collection will make, on average N x log(n) comparisons of your object using the IComparable.Compare method. Each of those will cause three boxing and unboxing operations. For an array with 1,000 points, that will be more than 20,000 boxing and unboxing operations, on average: N x log(n) is almost 7,000, and there are 3 box and unbox operations per comparison. You must look for better alt&#101;rnatives. You can&#39;t change the definition of IComparable.CompareTo(). But that doesn&#39;t mean you&#39;re forced to live with the performance costs of a weakly typed implementation for all your users. You can cr&#101;ate your own override of the CompareTo method that expects a Customer object:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct Customer : IComparable<br/>{<br/>&nbsp;&nbsp;private string _name;<br/>&nbsp;&nbsp;public Customer( string name )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_name = name;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#region IComparable Members<br/>&nbsp;&nbsp;// IComparable.CompareTo()<br/>&nbsp;&nbsp;// This is not type safe. The runtime type<br/>&nbsp;&nbsp;// of the right parameter must be checked.<br/>&nbsp;&nbsp;int IComparable.CompareTo( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( ! ( right is Customer ) )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new ArgumentException( &#34;Argument not a customer&#34;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;right&#34; );<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer rightCustomer = ( Customer )right;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return CompareTo( rightCustomer );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;// type-safe CompareTo.<br/>&nbsp;&nbsp;// Right is a customer, o&#114; derived from Customer.<br/>&nbsp;&nbsp;public int CompareTo( Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return _name.CompareTo( right._name );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#endregion<br/>}<br/>IComparable.CompareTo() is now an explicit interfaceimplementation; it can be called only through an IComparable reference. Users of your customer struct will get the type-safe comparison, and the unsafe comparison is inaccessible. The following innocent mistake no longer compiles:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Customer c1;<br/>Employee e1;<br/>if ( c1.CompareTo( e1 )&nbsp;&nbsp;&gt;&nbsp;&nbsp;0 )<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Customer one is greater&#34; );<br/>It does not compile because the arguments are wrong for the public Customer.CompareTo(Customer right) method. The IComparable. CompareTo(object right) method is not accessible. You can access the IComparable method only by explicitly casting the reference:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Customer c1;<br/>Employee e1;<br/>if ( ( c1 as IComparable ).CompareTo( e1 )&nbsp;&nbsp;&gt;&nbsp;&nbsp;0 )<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Customer one is greater&#34; );<br/>When you implement IComparable, use explicit interface implementation and provide a strongly typed public overload. The strongly typed overload improves performance and decreases the likelihood that someone will misuse the CompareTo method. You won&#39;t see all the benefits in the Sort function that the .NET Framework uses because it will still access CompareTo() tHRough the interface pointer (see Item 19), but code that knows the type of both objects being compared will get better performance.<br/>We&#39;ll make one last small change to the Customer struct. The C# language lets you overload the standard relational operators. Those should make use of the type-safe CompareTo() method:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct Customer : IComparable<br/>{<br/>&nbsp;&nbsp;private string _name;<br/>&nbsp;&nbsp;public Customer( string name )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_name = name;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#region IComparable Members<br/>&nbsp;&nbsp;// IComparable.CompareTo()<br/>&nbsp;&nbsp;// This is not type safe. The runtime type<br/>&nbsp;&nbsp;// of the right parameter must be checked.<br/>&nbsp;&nbsp;int IComparable.CompareTo( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( ! ( right is Customer ) )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new ArgumentException( &#34;Argument not a customer&#34;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;right&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer rightCustomer = ( Customer )right;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return CompareTo( rightCustomer );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;// type-safe CompareTo.<br/>&nbsp;&nbsp;// Right is a customer, o&#114; derived from Customer.<br/>&nbsp;&nbsp;public int CompareTo( Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return _name.CompareTo( right._name );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;// Relational Operators.<br/>&nbsp;&nbsp;public static bool operator &lt; ( Customer left,<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return left.CompareTo( right ) &lt; 0;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public static bool operator &lt;=( Customer left,<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return left.CompareTo( right ) &lt;= 0;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public static bool operator &gt;( Customer left,<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return left.CompareTo( right ) &gt; 0;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public static bool operator &gt;=( Customer left,<br/>&nbsp;&nbsp;&nbsp;&nbsp;Customer right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return left.CompareTo( right ) &gt;= 0;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;#endregion<br/>}<br/>That&#39;s all for the standard o&#114;der of customers: by name. Later, you must cr&#101;ate a report sorting all customers by revenue. You still need the normal comparison functionality defined by the Customer struct, sorting them by name. You can implement this additional o&#114;dering requirement by creating a class that implements the IComparer interface. IComparer provides the standard way to provide alt&#101;rnative o&#114;ders for a type. Any of the methods inside the .NET FCL that work on IComparable types provide overloads that o&#114;der objects through IComparer. Because you authored the Customer struct, you can cr&#101;ate this new class (RevenueComparer) as a private nested class inside the Customer struct. It gets exposed through a static property in the Customer struct:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct Customer : IComparable<br/>{<br/>&nbsp;&nbsp;private string _name;<br/>&nbsp;&nbsp;private double _revenue;<br/>&nbsp;&nbsp;// code from earlier example elided.<br/>&nbsp;&nbsp;private static RevenueComparer _revComp = null;<br/>&nbsp;&nbsp;// return an object that implements IComparer<br/>&nbsp;&nbsp;// use lazy evaluation to cr&#101;ate just one.<br/>&nbsp;&nbsp;public static IComparer RevenueCompare<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;get<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( _revComp == null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_revComp = new RevenueComparer();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return _revComp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;// Class to compare customers by revenue.<br/>&nbsp;&nbsp;// This is always used via the interface pointer,<br/>&nbsp;&nbsp;// so only provide the interface override.<br/>&nbsp;&nbsp;private class RevenueComparer : IComparer<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;#region IComparer Members<br/>&nbsp;&nbsp;&nbsp;&nbsp;int IComparer.Compare( object left, object right )<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( ! ( left is Customer ) )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new ArgumentException(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;Argument is not a Customer&#34;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;left&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (! ( right is Customer) )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new ArgumentException(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;Argument is not a Customer&#34;,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#34;right&#34;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer leftCustomer = ( Customer ) left;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Customer rightCustomer = ( Customer ) right;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return leftCustomer._revenue.CompareTo(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rightCustomer._revenue);<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;&nbsp;#endregion<br/>&nbsp;&nbsp;}<br/>}<br/>The last version of the Customer struct, with the embedded RevenueComparer, lets you o&#114;der a collection of customers by name, the natural o&#114;der for customers, and provides an alt&#101;rnative o&#114;der by exposing a class that implements the IComparer interface to o&#114;der customers by revenue. If you don&#39;t have access to the source for the Customer class, you can still provide an IComparer that o&#114;ders customers using any of its public properties. You should use that idiom only when you do not have access to the source for the class, as when you need a different o&#114;dering for one of the classes in the .NET Framework.<br/>Nowh&#101;re in this item did I mention Equals() o&#114; the == operator (see Item 9). o&#114;dering relations and equality are distinct operations. You do not need to implement an equality comparison to have an o&#114;dering relation. In fact, reference types commonly implement o&#114;dering based on the object contents, yet implement equality based on object identity. CompareTo() returns 0, even though Equals() returns false. That&#39;s perfectly legal. Equality and o&#114;dering relations are not necessarily the same.<br/>IComparable and IComparer are the standard mechanisms for providing o&#114;dering relations for your types. IComparable should be used for the most natural o&#114;dering. When you implement IComparable, you should overload the comparison operators (&lt;, &gt;, &lt;=, &gt;=) consistently with our IComparable o&#114;dering. IComparable.CompareTo() uses System.Object parameters, so you should also provide a type-specific overload of the CompareTo() method. IComparer can be used to provide alt&#101;rnative o&#114;derings o&#114; can be used when you need to provide o&#114;dering for a type that does not provide it for you.</p><p> + <a href="#CommentCard">&#x67E5;&#x770B;&#x5F53;&#x524D;&#x65E5;&#x5FD7;&#x8BC4;&#x8BBA;</a> (0)</p><p>&nbsp;<br/><br/><a href="wap.asp?do=Login">&#x767B;&#x5F55;</a></p><p><br/>&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;</p><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a></p><p><a href="http://www.pjhome.net/wap.asp">PJBlog3&nbsp;v3.2.9.518</a>&nbsp;Inside.</p><p>Processed&nbsp;In&nbsp;0.141&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card><card id="postCommentCard"><p><b>&#x6807;&#x9898;&#x3A;</b> <a href="#MainCard">&#x45;&#x66;&#x66;&#x65;&#x63;&#x74;&#x69;&#x76;&#x65;&#x20;&#x43;&#x23;&#x20;&#x539F;&#x5219;&#x32;&#x36;&#xFF1A;&#x7528;&#x49;&#x43;&#x6F;&#x6D;&#x70;&#x61;&#x72;&#x61;&#x62;&#x6C;&#x65;&#x548C;&#x49;&#x43;&#x6F;&#x6D;&#x70;&#x61;&#x72;&#x65;&#x72;&#x5B9E;&#x73B0;&#x5BF9;&#x8C61;&#x7684;&#x987A;&#x5E8F;&#x5173;&#x7CFB;</a></p><p><br/>你没有权限发表评论</p><p><br/>&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;</p><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a></p><p><a href="http://www.pjhome.net/wap.asp">PJBlog3&nbsp;v3.2.9.518</a>&nbsp;Inside.</p><p>Processed&nbsp;In&nbsp;0.141&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card><card id="CommentCard"><p>&#x6682;&#x65E0;&#x8BC4;&#x8BBA;</p><p><a href="#MainCard">&#x8FD4;&#x56DE;</a></p><p><br/>&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;</p><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a></p><p><a href="http://www.pjhome.net/wap.asp">PJBlog3&nbsp;v3.2.9.518</a>&nbsp;Inside.</p><p>Processed&nbsp;In&nbsp;0.141&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card>
</wml>
