
<?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;&#x39;&#xFF1A;&#x660E;&#x767D;&#x51E0;&#x4E2A;&#x76F8;&#x7B49;&#x8FD0;&#x7B97;&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x7CFB;</p><p><b>&#x4F5C;&#x8005;&#x3A;</b> &#x64;&#x6E;&#x61;&#x77;&#x6F;</p><p><b>&#x65E5;&#x671F;&#x3A;</b> &#x32;&#x30;&#x30;&#x39;&#x2D;&#x30;&#x36;&#x2D;&#x30;&#x33;&#x20;&#x31;&#x31;&#x3A;&#x33;&#x39;&#x20;&#x50;&#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> &#x660E;&#x767D;ReferenceEquals(), static Equals(), instance Equals(), &#x548C;&#x8FD0;&#x7B97;&#x884C;&#x7B26;==&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x7CFB;&#x3002;<br/>&#x5F53;&#x4F60;&#x521B;&#x5EFA;&#x4F60;&#x81EA;&#x5DF1;&#x7684;&#x7C7B;&#x578B;&#x65F6;(&#x4E0D;&#x7BA1;&#x662F;&#x7C7B;&#x8FD8;&#x662F;&#x7ED3;&#x6784;)&#xFF0C;&#x4F60;&#x8981;&#x5B9A;&#x4E49;&#x7C7B;&#x578B;&#x5728;&#x4EC0;&#x4E48;&#x60C5;&#x51B5;&#x4E0B;&#x662F;&#x76F8;&#x7B49;&#x7684;&#x3002;C#&#x63D0;&#x4F9B;&#x4E86;4&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x65AD;&#x5B9A;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F;&#x76F8;&#x7B49;&#x7684;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public static bool ReferenceEquals<br/>&nbsp;&nbsp;( object left, object right );<br/>public static bool Equals<br/>&nbsp;&nbsp;( object left, object right );<br/>public virtual bool Equals( object right);<br/>public static bool operator==( MyClass left, MyClass right );<br/>&#x8FD9;&#x79CD;&#x8BED;&#x8A00;&#x8BA9;&#x4F60;&#x53EF;&#x4EE5;&#x4E3A;&#x4E0A;&#x9762;&#x6240;&#x6709;&#x7684;4&#x79CD;&#x65B9;&#x6CD5;&#x521B;&#x5EFA;&#x81EA;&#x5DF1;&#x7684;&#x7248;&#x672C;&#x3002;But just because you can doesn&#39;t mean that you should.&#x4F60;&#x6216;&#x8BB8;&#x4ECE;&#x6765;&#x4E0D;&#x7528;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x524D;&#x9762;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x3002;&#x4F60;&#x7ECF;&#x5E38;&#x9047;&#x5230;&#x7684;&#x662F;&#x521B;&#x5EFA;&#x4F60;&#x81EA;&#x5DF1;&#x5B9E;&#x4F8B;&#x7684;Equals()&#x65B9;&#x6CD5;&#xFF0C;&#x6765;&#x4E3A;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x5B9A;&#x4E49;&#x8BED;&#x4E49;&#xFF1B;&#x6216;&#x8005;&#x4F60;&#x5076;&#x800C;&#x91CD;&#x8F7D;==&#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x4F46;&#x8FD9;&#x53EA;&#x662F;&#x4E3A;&#x4E86;&#x8003;&#x8651;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x6027;&#x80FD;&#x3002;&#x5E78;&#x8FD0;&#x7684;&#x662F;&#xFF0C;&#x8FD9;4&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x5173;&#x7CFB;&#xFF0C;&#x5F53;&#x4F60;&#x6539;&#x53D8;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x65F6;&#xFF0C;&#x4F1A;&#x5F71;&#x54CD;&#x5230;&#x5176;&#x5B83;&#x7684;&#x51E0;&#x4E2A;&#x3002;&#x662F;&#x7684;&#xFF0C;&#x987B;&#x8981;4&#x4E2A;&#x65B9;&#x6CD5;&#x6765;&#x5B8C;&#x6574;&#x7684;&#x6D4B;&#x8BD5;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x5B8C;&#x5168;&#x76F8;&#x7B49;&#x3002;&#x4F46;&#x4F60;&#x4E0D;&#x7528;&#x62C5;&#x5FC3;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x7B80;&#x5355;&#x7684;&#x641E;&#x5B9A;&#x5B83;&#x4EEC;&#x3002;<br/>&#x548C;C#&#x91CC;&#x5176;&#x5B83;&#x5927;&#x591A;&#x6570;&#x590D;&#x6742;&#x5143;&#x7D20;&#x4E00;&#x6837;&#xFF0C;&#x8FD9;&#x4E2A;(&#x5BF9;&#x76F8;&#x7B49;&#x7684;&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;)&#x4E5F;&#x9075;&#x5B88;&#x8FD9;&#x6837;&#x7684;&#x4E00;&#x4E2A;&#x4E8B;&#x5B9E;&#xFF1A;C#&#x5145;&#x8BB8;&#x4F60;&#x540C;&#x65F6;&#x521B;&#x5EFA;&#x503C;&#x7C7B;&#x578B;&#x548C;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x3002;&#x4E24;&#x4E2A;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x5728;&#x5F15;&#x7528;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x5B83;&#x4EEC;&#x662F;&#x76F8;&#x7B49;&#x7684;&#xFF0C;&#x5C31;&#x50CF;&#x5F15;&#x7528;&#x5230;&#x5BF9;&#x8C61;&#x7684;ID&#x4E00;&#x6837;&#x3002;&#x4E24;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x5728;&#x5B83;&#x4EEC;&#x7684;&#x7C7B;&#x578B;&#x548C;&#x5185;&#x5BB9;&#x90FD;&#x662F;&#x76F8;&#x540C;&#x65F6;&#xFF0C;&#x5B83;&#x4EEC;&#x5E94;&#x8BE5;&#x662F;&#x76F8;&#x7B49;&#x7684;&#x3002;&#x8FD9;&#x5C31;&#x662F;&#x4E3A;&#x4EC0;&#x4E48;&#x76F8;&#x7B49;&#x6D4B;&#x8BD5;&#x8981;&#x8FD9;&#x4E48;&#x591A;&#x65B9;&#x6CD5;&#x4E86;&#x3002;<br/>&#x6211;&#x4EEC;&#x5148;&#x4ECE;&#x4E24;&#x4E2A;&#x4F60;&#x53EF;&#x80FD;&#x4ECE;&#x6765;&#x4E0D;&#x4F1A;&#x4FEE;&#x6539;&#x7684;&#x65B9;&#x6CD5;&#x5F00;&#x59CB;&#x3002;Object.ReferenceEquals()&#x5728;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x5F15;&#x7528;&#x5230;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x65F6;&#x8FD4;&#x56DE;true&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x5177;&#x6709;&#x76F8;&#x540C;&#x7684;&#x5BF9;&#x8C61;ID&#x3002;&#x4E0D;&#x7BA1;&#x6BD4;&#x8F83;&#x7684;&#x7C7B;&#x578B;&#x662F;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x8FD8;&#x662F;&#x503C;&#x7C7B;&#x578B;&#x7684;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x603B;&#x662F;&#x68C0;&#x6D4B;&#x5BF9;&#x8C61;ID&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5BF9;&#x8C61;&#x5185;&#x5BB9;&#x3002;&#x662F;&#x7684;&#xFF0C;&#x8FD9;&#x5C31;&#x662F;&#x8BF4;&#x5F53;&#x4F60;&#x6D4B;&#x8BD5;&#x4E24;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#x662F;&#x5426;&#x76F8;&#x7B49;&#x65F6;&#xFF0C;ReferenceEquals()&#x603B;&#x4F1A;&#x8FD4;&#x56DE;false&#xFF0C;&#x5373;&#x4F7F;&#x4F60;&#x662F;&#x6BD4;&#x8F83;&#x540C;&#x4E00;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x4E5F;&#x4F1A;&#x8FD4;&#x56DE;false&#x3002;&#x8FD9;&#x91CC;&#x6709;&#x4E24;&#x4E2A;&#x88C5;&#x7BB1;&#xFF0C;&#x4F1A;&#x5728;&#x539F;&#x5219;16&#x4E2D;&#x8BA8;&#x8BBA;&#x3002;(&#x8BD1;&#x6CE8;&#xFF1A;&#x56E0;&#x4E3A;&#x53C2;&#x6570;&#x8981;&#x6C42;&#x4E24;&#x4E2A;&#x5F15;&#x7528;&#x5BF9;&#x8C61;&#xFF0C;&#x6240;&#x4EE5;&#x7528;&#x4E24;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#x6765;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;&#xFF0C;&#x4F1A;&#x5148;&#x4F7F;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x90FD;&#x88C5;&#x7BB1;&#xFF0C;&#x8FD9;&#x6837;&#x4E00;&#x6765;&#xFF0C;&#x4E24;&#x4E2A;&#x5F15;&#x7528; &#x5BF9;&#x8C61;&#x81EA;&#x7136;&#x5C31;&#x4E0D;&#x76F8;&#x7B49;&#x4E86;&#x3002;)<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;int i = 5;<br/>int j = 5;<br/>if ( Object.ReferenceEquals( i, j ))<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Never happens.&#34; );<br/>else<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Always happens.&#34; );<br/>if ( Object.ReferenceEquals( i, i ))<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Never happens.&#34; );<br/>else<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Always happens.&#34; );<br/>&#x4F60;&#x6216;&#x8BB8;&#x51B3;&#x4E0D;&#x4F1A;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;Object.ReferenceEquals()&#xFF0C;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x5B83;&#x5DF2;&#x7ECF;&#x786E;&#x5B9E;&#x5B9E;&#x73B0;&#x4E86;&#x5B83;&#x81EA;&#x5DF1;&#x7684;&#x529F;&#x80FD;&#xFF1A;&#x68C0;&#x6D4B;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x7684;&#x5BF9;&#x8C61;ID(&#x662F;&#x5426;&#x76F8;&#x540C;)&#x3002;<br/>&#x7B2C;&#x4E8C;&#x4E2A;&#x53EF;&#x80FD;&#x4ECE;&#x6765;&#x4E0D;&#x4F1A;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x9759;&#x6001;&#x7684;Object.Equals()&#x3002;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x5728;&#x4F60;&#x4E0D;&#x6E05;&#x695A;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x8FD0;&#x884C;&#x7C7B;&#x578B;&#x65F6;&#x4EC0;&#x4E48;&#x65F6;&#xFF0C;&#x68C0;&#x6D4B;&#x5B83;&#x4EEC;&#x662F;&#x5426;&#x76F8;&#x7B49;&#x3002;&#x8BB0;&#x4F4F;&#xFF1A;C#&#x91CC;System.Object&#x662F;&#x4E00;&#x5207;&#x5185;&#x5BB9;&#x7684;&#x6700;&#x7EC8;&#x57FA;&#x7C7B;&#x3002;&#x4EFB;&#x4F55;&#x65F6;&#x5019;&#x4F60;&#x5728;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x5B83;&#x4EEC;&#x90FD;&#x662F;System.Object&#x7684;&#x5B9E;&#x4F8B;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x5728;&#x4E0D;&#x77E5;&#x9053;&#x5B83;&#x4EEC;&#x7684;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x800C;&#x7B49;&#x5F0F;&#x7684;&#x6539;&#x53D8;&#x53C8;&#x662F;&#x4F9D;&#x61D2;&#x4E8E;&#x7C7B;&#x578B;&#x7684;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x662F;&#x600E;&#x6837;&#x6765;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x662F;&#x5426;&#x76F8;&#x7B49;&#x7684;&#x5462;&#xFF1F;&#x7B54;&#x6848;&#x5F88;&#x7B80;&#x5355;&#xFF1A;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x628A;&#x6BD4;&#x8F83;&#x7684;&#x804C;&#x8D23;&#x59D4;&#x4EA4;&#x7ED9;&#x4E86;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x6B63;&#x5728;&#x6BD4;&#x8F83;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x9759;&#x6001;&#x7684;Object.Equals()&#x65B9;&#x6CD5;&#x662F;&#x50CF;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x5B9E;&#x73B0;&#x7684;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public static bool Equals( object left, object right )<br/>{<br/>&nbsp;&nbsp;// Check object identity<br/>&nbsp;&nbsp;if (left == right )<br/>&nbsp;&nbsp;&nbsp;&nbsp;return true;<br/>&nbsp;&nbsp;// both null references handled above<br/>&nbsp;&nbsp;if ((left == null) || (right == null))<br/>&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;return left.Equals (right);<br/>}<br/>&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x5C55;&#x793A;&#x7684;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x662F;&#x6211;&#x8FD8;&#x6CA1;&#x6709;&#x8BA8;&#x8BBA;&#x7684;&#xFF1A;&#x64CD;&#x4F5C;&#x7B26;==()&#x548C;&#x5B9E;&#x4F8B;&#x7684;Equals()&#x65B9;&#x6CD5;&#x3002;&#x6211;&#x4F1A;&#x8BE6;&#x7EC6;&#x7684;&#x89E3;&#x91CA;&#x8FD9;&#x4E24;&#x4E2A;&#xFF0C;&#x4F46;&#x6211;&#x8FD8;&#x6CA1;&#x6709;&#x51C6;&#x5907;&#x7ED3;&#x675F;&#x5BF9;&#x9759;&#x6001;&#x7684;Equals()&#x7684;&#x8BA8;&#x8BBA;&#x3002;&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x5E0C;&#x671B;&#x4F60;&#x660E;&#x767D;&#xFF0C;&#x9759;&#x6001;&#x7684;Equals()&#x662F;&#x4F7F;&#x7528;&#x5DE6;&#x8FB9;&#x53C2;&#x6570;&#x5B9E;&#x4F8B;&#x7684;Equals()&#x65B9;&#x6CD5;&#x6765;&#x65AD;&#x5B9A;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x76F8;&#x7B49;&#x3002;<br/>&#x4E0E;ReferenceEquals()&#x4E00;&#x6837;&#xFF0C;&#x4F60;&#x6216;&#x8BB8;&#x4ECE;&#x6765;&#x4E0D;&#x4F1A;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x9759;&#x6001;&#x7684;Object.Equals()&#x65B9;&#x6CD5;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x5DF2;&#x7ECF;&#x786E;&#x5B9E;&#x7684;&#x5B8C;&#x6210;&#x4E86;&#x5B83;&#x5E94;&#x8BE5;&#x5B8C;&#x6210;&#x7684;&#x4E8B;&#xFF1A;&#x5728;&#x4F60;&#x4E0D;&#x77E5;&#x9053;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x786E;&#x5207;&#x7C7B;&#x578B;&#x65F6;&#x65AD;&#x5B9A;&#x5B83;&#x4EEC;&#x662F;&#x5426;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;&#x56E0;&#x4E3A;&#x9759;&#x6001;&#x7684;Equals()&#x65B9;&#x6CD5;&#x628A;&#x6BD4;&#x8F83;&#x59D4;&#x6258;&#x7ED9;&#x5DE6;&#x8FB9;&#x53C2;&#x6570;&#x5B9E;&#x4F8B;&#x7684;Equals()&#xFF0C;&#x5B83;&#x5C31;&#x662F;&#x7528;&#x8FD9;&#x4E00;&#x539F;&#x5219;&#x6765;&#x5904;&#x7406;&#x53E6;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x7684;&#x3002;<br/>&#x73B0;&#x5728;&#x4F60;&#x5E94;&#x8BE5;&#x660E;&#x767D;&#x4E3A;&#x4EC0;&#x4E48;&#x4F60;&#x4ECE;&#x6765;&#x4E0D;&#x5FC5;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x9759;&#x6001;&#x7684;ReferenceEquals()&#x4EE5;&#x53CA;&#x9759;&#x6001;&#x7684;Equals()&#x65B9;&#x6CD5;&#x4E86;&#x5427;&#x3002;&#x73B0;&#x5728;&#x6765;&#x8BA8;&#x8BBA;&#x4F60;&#x987B;&#x8981;&#x91CD;&#x8F7D;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x4F46;&#x9996;&#x5148;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x5148;&#x6765;&#x8BA8;&#x8BBA;&#x4E00;&#x4E0B;&#x8FD9;&#x6837;&#x7684;&#x4E00;&#x4E2A;&#x4E0E;&#x76F8;&#x7B49;&#x76F8;&#x5173;&#x7684;&#x6570;&#x5B66;&#x6027;&#x8D28;&#x3002;&#x4F60;&#x5FC5;&#x987B;&#x786E;&#x4FDD;&#x4F60;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x7684;&#x65B9;&#x6CD5;&#x7684;&#x5B9E;&#x73B0;&#x8981;&#x4E0E;&#x5176;&#x5B83;&#x7A0B;&#x5E8F;&#x5458;&#x6240;&#x671F;&#x671B;&#x7684;&#x5B9E;&#x73B0;&#x662F;&#x4E00;&#x81F4;&#x7684;&#x3002;&#x8FD9;&#x5C31;&#x662F;&#x8BF4;&#x4F60;&#x5FC5;&#x987B;&#x786E;&#x4FDD;&#x8FD9;&#x6837;&#x7684;&#x4E00;&#x4E2A;&#x6570;&#x5B66;&#x76F8;&#x7B49;&#x6027;&#x8D28;&#xFF1A;&#x76F8;&#x7B49;&#x7684;&#x81EA;&#x53CD;&#x6027;&#xFF0C;&#x5BF9;&#x79F0;&#x6027;&#x548C;&#x4F20;&#x9012;&#x6027;&#x3002;&#x81EA;&#x53CD;&#x6027;&#x5C31;&#x662F;&#x8BF4;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x7B49;&#x4E8E;&#x5B83;&#x81EA;&#x5DF1;&#x7684;&#xFF0C;&#x4E0D;&#x7BA1;&#x5BF9;&#x4E8E;&#x4EC0;&#x4E48;&#x7C7B;&#x578B;&#xFF0C;a==a&#x603B;&#x5E94;&#x8BE5;&#x8FD4;&#x56DE;true&#xFF1B;&#x5BF9;&#x79F0;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5982;&#x679C;&#x6709;a==b&#x4E3A;&#x771F;&#xFF0C;&#x90A3;&#x4E48;b==a&#x4E5F;&#x5FC5;&#x987B;&#x4E3A;&#x771F;&#xFF1B;&#x4F20;&#x9012;&#x6027;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5982;&#x679C;a==b&#x4E3A;&#x771F;&#xFF0C;&#x4E14;b==c&#x4E5F;&#x4E3A;&#x771F;&#xFF0C;&#x90A3;&#x4E48;a==c&#x4E5F;&#x5FC5;&#x987B;&#x4E3A;&#x771F;&#xFF0C;&#x8FD9;&#x5C31;&#x662F;&#x4F20;&#x9012;&#x6027;&#x3002;<br/>&#x73B0;&#x5728;&#x662F;&#x65F6;&#x5019;&#x6765;&#x8BA8;&#x8BBA;&#x5B9E;&#x4F8B;&#x7684;Object.Equals()&#x51FD;&#x6570;&#x4E86;&#xFF0C;&#x5305;&#x62EC;&#x4F60;&#x5E94;&#x8BE5;&#x5728;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x6765;&#x91CD;&#x8F7D;&#x5B83;&#x3002;&#x5F53;&#x9ED8;&#x8BA4;&#x7684;&#x884C;&#x4E3A;&#x4E0E;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x4E0D;&#x4E00;&#x81F4;&#x65F6;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x521B;&#x5EFA;&#x4F60;&#x81EA;&#x5DF1;&#x7684;&#x5B9E;&#x4F8B;&#x7248;&#x672C;&#x3002;Object.Equals()&#x65B9;&#x6CD5;&#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x7684;ID&#x6765;&#x65AD;&#x5B9A;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x662F;&#x5426;&#x76F8;&#x7B49;&#x3002;&#x8FD9;&#x4E2A;&#x9ED8;&#x8BA4;&#x7684;Object.Equals()&#x51FD;&#x6570;&#x7684;&#x884C;&#x4E3A;&#x4E0E;Object.ReferenceEquals()&#x786E;&#x5B9E;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;&#x4F46;&#x662F;&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x503C;&#x7C7B;&#x578B;&#x662F;&#x4E0D;&#x4E00;&#x6837;&#x7684;&#x3002;System.ValueType&#x5E76;&#x6CA1;&#x6709;&#x91CD;&#x8F7D;Object.Equals()&#xFF0C;&#x8BB0;&#x4F4F;&#xFF0C;System.ValueType&#x662F;&#x6240;&#x6709;&#x4F60;&#x6240;&#x521B;&#x5EFA;&#x7684;&#x503C;&#x7C7B;&#x578B;(&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;struct&#x521B;&#x5EFA;)&#x7684;&#x57FA;&#x7C7B;&#x3002;&#x4E24;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x76F8;&#x7B49;&#xFF0C;&#x5982;&#x679C;&#x5B83;&#x4EEC;&#x7684;&#x7C7B;&#x578B;&#x548C;&#x5185;&#x5BB9;&#x90FD;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;ValueType.Equals()&#x5B9E;&#x73B0;&#x4E86;&#x8FD9;&#x4E00;&#x884C;&#x4E3A;&#x3002;&#x4E0D;&#x5E78;&#x7684;&#x662F;&#xFF0C;ValueType.Equals()&#x5E76;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x9AD8;&#x6548;&#x7684;&#x5B9E;&#x73B0;&#x3002;ValueType.Equals()&#x662F;&#x6240;&#x6709;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x57FA;&#x7C7B;(&#x8BD1;&#x6CE8;&#xFF1A;&#x8FD9;&#x91CC;&#x662F;&#x8BF4;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x5728;&#x57FA;&#x7C7B;&#x4E0A;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;)&#x3002;&#x4E3A;&#x4E86;&#x63D0;&#x4F9B;&#x6B63;&#x786E;&#x7684;&#x884C;&#x4E3A;&#xFF0C;&#x5B83;&#x5FC5;&#x987B;&#x6BD4;&#x8F83;&#x6D3E;&#x751F;&#x7C7B;&#x7684;&#x6240;&#x6709;&#x6210;&#x5458;&#x53D8;&#x91CF;&#xFF0C;&#x800C;&#x4E14;&#x662F;&#x5728;&#x4E0D;&#x77E5;&#x9053;&#x6D3E;&#x751F;&#x7C7B;&#x7684;&#x7C7B;&#x578B;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x3002;&#x5728;C#&#x91CC;&#xFF0C;&#x8FD9;&#x5C31;&#x610F;&#x5473;&#x7740;&#x8981;&#x4F7F;&#x7528;&#x53CD;&#x5C04;&#x3002;&#x6B63;&#x5982;&#x4F60;&#x5C06;&#x4F1A;&#x5728;&#x539F;&#x5219;44&#x91CC;&#x770B;&#x5230;&#x7684;&#xFF0C;&#x5BF9;&#x53CD;&#x5C04;&#x800C;&#x8A00;&#x5B83;&#x4EEC;&#x6709;&#x592A;&#x591A;&#x7684;&#x4E0D;&#x5229;&#x4E4B;&#x5904;&#xFF0C;&#x7279;&#x522B;&#x662F;&#x5728;&#x4EE5;&#x6027;&#x80FD;&#x4E3A;&#x76EE;&#x6807;&#x7684;&#x65F6;&#x5019;&#x3002;<br/>&#x76F8;&#x7B49;&#x662F;&#x5728;&#x5E94;&#x7528;&#x4E2D;&#x7ECF;&#x5E38;&#x8C03;&#x7528;&#x7684;&#x57FA;&#x7840;&#x7ED3;&#x6784;&#x4E4B;&#x4E00;&#xFF0C;&#x56E0;&#x6B64;&#x6027;&#x80FD;&#x5E94;&#x8BE5;&#x662F;&#x503C;&#x5F97;&#x8003;&#x8651;&#x7684;&#x76EE;&#x6807;&#x3002;&#x5728;&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4E3A;&#x4F60;&#x7684;&#x4EFB;&#x4F55;&#x503C;&#x7C7B;&#x578B;&#x91CD;&#x8F7D;&#x4E00;&#x4E2A;&#x5FEB;&#x5F97;&#x591A;&#x7684;Equals()&#x3002;&#x7B80;&#x5355;&#x7684;&#x63A8;&#x8350;&#x4E00;&#x4E0B;&#xFF1A;&#x5728;&#x4F60;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x603B;&#x662F;&#x91CD;&#x8F7D;ValueType.Equals()&#x3002;<br/>&#x4F60;&#x5E94;&#x8BE5;&#x91CD;&#x8F7D;&#x5B9E;&#x4F8B;&#x7684;Equals()&#x51FD;&#x6570;&#xFF0C;&#x4EC5;&#x5F53;&#x4F60;&#x60F3;&#x6539;&#x53D8;&#x4E00;&#x4E2A;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x6240;&#x5B9A;&#x4E49;&#x7684;(Equals()&#x7684;)&#x8BED;&#x4E49;&#x65F6;&#x3002;.Net&#x7ED3;&#x6784;&#x7C7B;&#x5E93;&#x4E2D;&#x5927;&#x91CF;&#x7684;&#x7C7B;&#x662F;&#x4F7F;&#x7528;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x8BED;&#x4E49;&#x6765;&#x4EE3;&#x66FF;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x7684;&#x8BED;&#x4E49;&#x3002;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;&#x4E2D;&#x5BF9;&#x8C61;&#x76F8;&#x7B49;&#xFF0C;&#x5982;&#x679C;&#x5B83;&#x4EEC;&#x5305;&#x542B;&#x76F8;&#x540C;&#x7684;&#x5185;&#x5BB9;&#x3002;&#x4E24;&#x4E2A;DataRowViewc&#x5BF9;&#x8C61;&#x76F8;&#x7B49;&#xFF0C;&#x5982;&#x679C;&#x5B83;&#x4EEC;&#x5F15;&#x7528;&#x5230;&#x540C;&#x4E00;&#x4E2A;DataRow&#x3002;&#x5173;&#x952E;&#x5C31;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x987B;&#x8981;&#x9075;&#x4ECE;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x8BED;&#x4E49;(&#x6BD4;&#x8F83;&#x5185;&#x5BB9;)&#x800C;&#x4E0D;&#x662F;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x7684;&#x8BED;&#x4E49;(&#x6BD4;&#x8F83;&#x5BF9;&#x8C61;ID)&#x65F6;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x81EA;&#x5DF1;&#x91CD;&#x8F7D;&#x5B9E;&#x4F8B;&#x7684;Object.Equals()&#x65B9;&#x6CD5;&#x3002;<br/>&#x597D;&#x4E86;&#xFF0C;&#x73B0;&#x5728;&#x4F60;&#x77E5;&#x9053;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x5E94;&#x8BE5;&#x91CD;&#x8F7D;&#x4F60;&#x81EA;&#x5DF1;&#x7684;Object.Equals()&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x660E;&#x767D;&#x600E;&#x6837;&#x6765;&#x5B9E;&#x73B0;&#x5B83;&#x3002;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x6BD4;&#x8F83;&#x5173;&#x7CFB;&#x6709;&#x5F88;&#x591A;&#x88C5;&#x7BB1;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x88C5;&#x7BB1;&#x5728;&#x539F;&#x5219;17&#x4E2D;&#x8BA8;&#x8BBA;&#x3002;&#x5BF9;&#x4E8E;&#x7528;&#x6237;&#x7C7B;&#x578B;&#xFF0C;&#x4F60;&#x7684;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x987B;&#x8981;&#x9075;&#x4ECE;&#x539F;&#x5148;&#x5B9A;&#x4E49;&#x884C;&#x4E3A;(&#x8BD1;&#x6CE8;&#xFF1A;&#x524D;&#x9762;&#x7684;&#x6570;&#x5B66;&#x76F8;&#x7B49;&#x6027;&#x8D28;)&#xFF0C;&#x4ECE;&#x800C;&#x907F;&#x514D;&#x4F60;&#x7684;&#x7528;&#x6237;&#x5728;&#x4F7F;&#x7528;&#x4F60;&#x7684;&#x7C7B;&#x65F6;&#x53D1;&#x751F;&#x4E00;&#x4E9B;&#x610F;&#x60F3;&#x4E0D;&#x5230;&#x7684;&#x884C;&#x4E3A;&#x3002;&#x8FD9;&#x6709;&#x4E00;&#x4E2A;&#x6807;&#x51C6;&#x7684;&#x6A21;&#x5F0F;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class Foo<br/>{<br/>&nbsp;&nbsp;public override bool Equals( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;// check null:<br/>&nbsp;&nbsp;&nbsp;&nbsp;// the this pointer is never null in C# methods.<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (right == null)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (object.ReferenceEquals( this, right ))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Discussed below.<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (this.GetType() != right.GetType())<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Compare this type&#39;s contents here:<br/>&nbsp;&nbsp;&nbsp;&nbsp;return CompareFooMembers(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this, right as Foo );<br/>&nbsp;&nbsp;}<br/>}<br/>&#x9996;&#x5148;&#xFF0C;Equals()&#x51B3;&#x4E0D;&#x5E94;&#x8BE5;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x8FD9;&#x611F;&#x89C9;&#x4E0D;&#x5927;&#x597D;&#x3002;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x8981;&#x4E48;&#x76F8;&#x7B49;&#xFF0C;&#x8981;&#x4E48;&#x4E0D;&#x7B49;&#xFF1B;&#x6CA1;&#x6709;&#x5176;&#x5B83;&#x5931;&#x8D25;&#x7684;&#x4F59;&#x5730;&#x3002;&#x76F4;&#x63A5;&#x4E3A;&#x6240;&#x6709;&#x7684;&#x5931;&#x8D25;&#x8FD4;&#x56DE;false&#xFF0C;&#x4F8B;&#x5982;null&#x5F15;&#x7528;&#x6216;&#x8005;&#x9519;&#x8BEF;&#x53C2;&#x6570;&#x3002;&#x73B0;&#x5728;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x6DF1;&#x5165;&#x7684;&#x8BA8;&#x8BBA;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x7EC6;&#x8282;&#xFF0C;&#x8FD9;&#x6837;&#x4F60;&#x4F1A;&#x660E;&#x767D;&#x4E3A;&#x4EC0;&#x4E48;&#x6BCF;&#x4E2A;&#x68C0;&#x6D4B;&#x4E3A;&#x4EC0;&#x4E48;&#x4F1A;&#x5728;&#x90A3;&#x91CC;&#xFF0C;&#x4EE5;&#x53CA;&#x90A3;&#x4E9B;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x68C0;&#x6D4B;&#x65AD;&#x5B9A;&#x53F3;&#x8FB9;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x4E3A;null&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x5F15;&#x7528;&#x4E0A;&#x6CA1;&#x6709;&#x65B9;&#x6CD5;&#x68C0;&#x6D4B;&#xFF0C;&#x5728;C#&#x91CC;&#xFF0C;&#x8FD9;&#x51B3;&#x4E0D;&#x53EF;&#x80FD;&#x4E3A;null&#x3002;&#x5728;&#x4F60;&#x8C03;&#x7528;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x5F15;&#x7528;&#x5230;null&#x7684;&#x5B9E;&#x4F8B;&#x7684;&#x65B9;&#x6CD5;&#x4E4B;&#x524D;&#xFF0C;CLR&#x53EF;&#x80FD;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x3002;&#x4E0B;&#x4E00;&#x6B65;&#x7684;&#x68C0;&#x6D4B;&#x6765;&#x65AD;&#x5B9A;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x662F;&#x5426;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x68C0;&#x6D4B;&#x5BF9;&#x8C61;ID&#x5C31;&#x884C;&#x4E86;&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x9AD8;&#x6548;&#x7684;&#x68C0;&#x6D4B;&#xFF0C;&#x5E76;&#x4E14;&#x76F8;&#x7B49;&#x7684;&#x5BF9;&#x8C61;ID&#x6765;&#x4FDD;&#x8BC1;&#x76F8;&#x540C;&#x7684;&#x5185;&#x5BB9;&#x3002;<br/>&#x63A5;&#x4E0B;&#x6765;&#x7684;&#x68C0;&#x6D4B;&#x6765;&#x65AD;&#x5B9A;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F;&#x540C;&#x6837;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;&#x8FD9;&#x4E2A;&#x6B65;&#x9AA4;&#x662F;&#x5F88;&#x91CD;&#x8981;&#x7684;&#xFF0C;&#x9996;&#x5148;&#xFF0C;&#x5E94;&#x8BE5;&#x6CE8;&#x610F;&#x5230;&#x5B83;&#x5E76;&#x4E0D;&#x4E00;&#x5B9A;&#x662F;Foo&#x7C7B;&#x578B;&#xFF0C;&#x5B83;&#x8C03;&#x7528;&#x4E86;this.GetType()&#xFF0C;&#x8FD9;&#x4E2A;&#x5B9E;&#x9645;&#x7684;&#x7C7B;&#x578B;&#x53EF;&#x80FD;&#x662F;&#x4ECE;Foo&#x7C7B;&#x6D3E;&#x751F;&#x7684;&#x3002;&#x5176;&#x6B21;&#xFF0C;&#x8FD9;&#x91CC;&#x7684;&#x4EE3;&#x7801;&#x5728;&#x6BD4;&#x8F83;&#x524D;&#x68C0;&#x6D4B;&#x4E86;&#x5BF9;&#x8C61;&#x7684;&#x786E;&#x5207;&#x7C7B;&#x578B;&#x3002;&#x8FD9;&#x5E76;&#x4E0D;&#x80FD;&#x5145;&#x5206;&#x4FDD;&#x8BC1;&#x4F60;&#x53EF;&#x4EE5;&#x628A;&#x53F3;&#x8FB9;&#x7684;&#x53C2;&#x6570;&#x8F6C;&#x5316;&#x6210;&#x5F53;&#x524D;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x8FD9;&#x4E2A;&#x6D4B;&#x8BD5;&#x4F1A;&#x4EA7;&#x751F;&#x4E24;&#x4E2A;&#x7EC6;&#x5FAE;&#x7684;BUG&#x3002;&#x8003;&#x8651;&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x7B80;&#x5355;&#x7EE7;&#x627F;&#x5C42;&#x6B21;&#x5173;&#x7CFB;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class B<br/>{<br/>&nbsp;&nbsp;public override bool Equals( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;// check null:<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (right == null)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Check reference equality:<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (object.ReferenceEquals( this, right ))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Problems here, discussed below.<br/>&nbsp;&nbsp;&nbsp;&nbsp;B rightAsB = right as B;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (rightAsB == null)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return CompareBMembers( this, rightAsB );<br/>&nbsp;&nbsp;}<br/>}<br/>public class D : B<br/>{<br/>&nbsp;&nbsp;// etc.<br/>&nbsp;&nbsp;public override bool Equals( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;// check null:<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (right == null)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (object.ReferenceEquals( this, right ))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Problems here.<br/>&nbsp;&nbsp;&nbsp;&nbsp;D rightAsD = right as D;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (rightAsD == null)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (base.Equals( rightAsD ) == false)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return CompareDMembers( this, rightAsD );<br/>&nbsp;&nbsp;}<br/>}<br/>//Test:<br/>B baseObject = new B();<br/>D derivedObject = new D();<br/>// Comparison 1.<br/>if (baseObject.Equals(derivedObject))<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Equals&#34; );<br/>else<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Not Equal&#34; );<br/>// Comparison 2.<br/>if (derivedObject.Equals(baseObject))<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Equals&#34; );<br/>else<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Not Equal&#34; );<br/>&#x5728;&#x4EFB;&#x4F55;&#x53EF;&#x80FD;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4F60;&#x90FD;&#x5E0C;&#x671B;&#x8981;&#x4E48;&#x770B;&#x5230;&#x4E24;&#x4E2A;Equals&#x6216;&#x8005;&#x4E24;&#x4E2A;Not Equal&#x3002;&#x56E0;&#x4E3A;&#x4E00;&#x4E9B;&#x9519;&#x8BEF;&#xFF0C;&#x8FD9;&#x5E76;&#x4E0D;&#x662F;&#x5148;&#x524D;&#x4EE3;&#x7801;&#x7684;&#x60C5;&#x5F62;&#x3002;&#x8FD9;&#x91CC;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x6BD4;&#x8F83;&#x51B3;&#x4E0D;&#x4F1A;&#x8FD4;&#x56DE;true&#x3002;&#x8FD9;&#x91CC;&#x7684;&#x57FA;&#x7C7B;&#xFF0C;&#x7C7B;&#x578B;B&#xFF0C;&#x51B3;&#x4E0D;&#x53EF;&#x80FD;&#x8F6C;&#x5316;&#x4E3A;D&#x3002;&#x7136;&#x800C;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x6BD4;&#x8F83;&#x53EF;&#x80FD;&#x8FD4;&#x56DE;true&#x3002;&#x6D3E;&#x751F;&#x7C7B;&#xFF0C;&#x7C7B;&#x578B;D&#xFF0C;&#x53EF;&#x4EE5;&#x9690;&#x5F0F;&#x7684;&#x8F6C;&#x5316;&#x4E3A;&#x7C7B;&#x578B;B&#x3002;&#x5982;&#x679C;&#x53F3;&#x8FB9;&#x53C2;&#x6570;&#x4EE5;B&#x7C7B;&#x578B;&#x5C55;&#x793A;&#x7684;&#x6210;&#x5458;&#x4E0E;&#x5DE6;&#x8FB9;&#x53C2;&#x6570;&#x4EE5;B&#x7C7B;&#x578B;&#x5C55;&#x793A;&#x7684;&#x6210;&#x5458;&#x662F;&#x540C;&#x7B49;&#x7684;&#xFF0C;B.Equals()&#x5C31;&#x8BA4;&#x4E3A;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x76F8;&#x7B49;&#x7684;&#x3002;&#x4F60;&#x5C06;&#x7834;&#x574F;&#x76F8;&#x7B49;&#x7684;&#x5BF9;&#x79F0;&#x6027;&#x3002;&#x8FD9;&#x4E00;&#x67B6;&#x6784;&#x88AB;&#x7834;&#x574F;&#x662F;&#x56E0;&#x4E3A;&#x81EA;&#x52A8;&#x5B9E;&#x73B0;&#x4E86;&#x5728;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#x4E2D;&#x9690;&#x5F0F;&#x7684;&#x4E0A;&#x4E0B;&#x8F6C;&#x5316;&#x3002;<br/>&#x5F53;&#x4F60;&#x8FD9;&#x6837;&#x5199;&#x65F6;&#xFF0C;&#x7C7B;&#x578B;D&#x88AB;&#x9690;&#x5F0F;&#x7684;&#x8F6C;&#x5316;&#x4E3A;B&#x7C7B;&#x578B;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;baseObject.Equals( derived )<br/>&#x5982;&#x679C;baseObject.Equals()&#x5728;&#x5B83;&#x81EA;&#x5DF1;&#x6240;&#x5B9A;&#x4E49;&#x7684;&#x6210;&#x5458;&#x76F8;&#x7B49;&#x65F6;&#xFF0C;&#x5C31;&#x65AD;&#x5B9A;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x76F8;&#x7B49;&#x7684;&#x3002;&#x53E6;&#x4E00;&#x65B9;&#x9762;&#xFF0C;&#x5F53;&#x4F60;&#x8FD9;&#x6837;&#x5199;&#x65F6;&#xFF0C;&#x7C7B;&#x578B;B&#x4E0D;&#x80FD;&#x8F6C;&#x5316;&#x4E3A;D&#x7C7B;&#x578B;&#xFF0C;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;derivedObject.Equals( base )<br/>B&#x5BF9;&#x8C61;&#x4E0D;&#x80FD;&#x8F6C;&#x5316;&#x4E3A;D&#x5BF9;&#x8C61;&#xFF0C;derivedObject.Equals()&#x65B9;&#x6CD5;&#x603B;&#x662F;&#x8FD4;&#x56DE;false&#x3002;&#x5982;&#x679C;&#x4F60;&#x4E0D;&#x786E;&#x5207;&#x7684;&#x68C0;&#x6D4B;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x4E00;&#x4E0D;&#x5C0F;&#x5FC3;&#x5C31;&#x9677;&#x5165;&#x8FD9;&#x6837;&#x7684;&#x7A98;&#x5883;&#xFF0C;&#x6BD4;&#x8F83;&#x5BF9;&#x8C61;&#x7684;&#x987A;&#x5E8F;&#x6210;&#x4E3A;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#x3002;<br/>&#x5F53;&#x4F60;&#x91CD;&#x8F7D;Equals()&#x65F6;&#xFF0C;&#x8FD9;&#x91CC;&#x8FD8;&#x6709;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x53EF;&#x884C;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x4F60;&#x5E94;&#x8BE5;&#x8C03;&#x7528;&#x57FA;&#x7C7B;&#x7684;System.Object&#x6216;&#x8005;System.ValueType&#x7684;&#x6BD4;&#x8F83;&#x65B9;&#x6CD5;&#xFF0C;&#x9664;&#x975E;&#x57FA;&#x7C7B;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#x5B83;&#x3002;&#x524D;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;&#x3002;&#x7C7B;&#x578B;D&#x8C03;&#x7528;&#x57FA;&#x7C7B;&#xFF0C;&#x7C7B;&#x578B;B&#xFF0C;&#x5B9A;&#x4E49;&#x7684;Equals()&#x65B9;&#x6CD5;&#xFF0C;&#x7136;&#x800C;&#xFF0C;&#x7C7B;B&#x6CA1;&#x6709;&#x8C03;&#x7528;baseObject.Equals()&#x3002;&#x5B83;&#x8C03;&#x7528;&#x4E86;Systme.Object&#x91CC;&#x5B9A;&#x4E49;&#x7684;&#x90A3;&#x4E2A;&#x7248;&#x672C;&#xFF0C;&#x5C31;&#x662F;&#x5F53;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x5F15;&#x7528;&#x5230;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x65F6;&#x5B83;&#x8FD4;&#x56DE;true&#x3002;&#x8FD9;&#x5E76;&#x4E0D;&#x662F;&#x4F60;&#x60F3;&#x8981;&#x7684;&#xFF0C;&#x6216;&#x8005;&#x4F60;&#x662F;&#x8FD8;&#x6CA1;&#x6709;&#x5728;&#x7B2C;&#x4E00;&#x4E2A;&#x7C7B;&#x91CC;&#x7684;&#x5199;&#x4F60;&#x81EA;&#x5DF1;&#x7684;&#x65B9;&#x6CD5;&#x3002;<br/>&#x539F;&#x5219;&#x662F;&#x4E0D;&#x7BA1;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#xFF0C;&#x5728;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#x65F6;&#x91CD;&#x8F7D;Equals()&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x4E14;&#x4F60;&#x4E0D;&#x60F3;&#x8BA9;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x9075;&#x4ECE;&#x9ED8;&#x8BA4;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x7684;&#x8BED;&#x4E49;&#x65F6;&#x4E5F;&#x91CD;&#x8F7D;Equals(),&#x5C31;&#x50CF;System.Object&#x5B9A;&#x4E49;&#x7684;&#x90A3;&#x6837;&#x3002;&#x5F53;&#x4F60;&#x5199;&#x4F60;&#x81EA;&#x5DF1;&#x7684;Equals()&#x65F6;&#xFF0C;&#x9075;&#x4ECE;&#x8981;&#x70B9;&#x91CC;&#x5B9E;&#x73B0;&#x7684;&#x5185;&#x5BB9;&#x3002;&#x91CD;&#x8F7D;Equals()&#x5C31;&#x610F;&#x5473;&#x7740;&#x4F60;&#x5E94;&#x8BE5;&#x91CD;&#x5199;GetHashCode()&#xFF0C;&#x8BE6;&#x60C5;&#x53C2;&#x89C1;&#x539F;&#x5219;10&#x3002;<br/>&#x89E3;&#x51B3;&#x4E86;&#x4E09;&#x4E2A;&#xFF0C;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#xFF1A;&#x64CD;&#x4F5C;&#x7B26;==()&#xFF0C;&#x4EFB;&#x4F55;&#x65F6;&#x5019;&#x4F60;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#xFF0C;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x64CD;&#x4F5C;&#x7B26;==()&#x3002;&#x539F;&#x56E0;&#x548C;&#x5B9E;&#x4F8B;&#x7684;Equals()&#x662F;&#x5B8C;&#x5168;&#x4E00;&#x6837;&#x7684;&#x3002;&#x9ED8;&#x8BA4;&#x7684;&#x7248;&#x672C;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x5F15;&#x7528;&#x7684;&#x6BD4;&#x8F83;&#x6765;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#x3002;&#x6548;&#x7387;&#x8FDC;&#x4E0D;&#x53CA;&#x4F60;&#x81EA;&#x5DF1;&#x4EFB;&#x610F;&#x5B9E;&#x73B0;&#x7684;&#x4E00;&#x4E2A;&#xFF0C;&#x6240;&#x4EE5;&#xFF0C;&#x4F60;&#x81EA;&#x5DF1;&#x5199;&#x3002;&#x5F53;&#x4F60;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x9075;&#x4ECE;&#x539F;&#x5219;17&#x91CC;&#x7684;&#x5EFA;&#x8BAE;&#x6765;&#x907F;&#x514D;&#x88C5;&#x7BB1;&#x3002;<br/>&#x6CE8;&#x610F;&#xFF0C;&#x6211;&#x5E76;&#x4E0D;&#x662F;&#x8BF4;&#x4E0D;&#x7BA1;&#x4F60;&#x662F;&#x5426;&#x91CD;&#x8F7D;&#x4E86;&#x5B9E;&#x4F8B;&#x7684;Equals()&#xFF0C;&#x90FD;&#x8FD8;&#x8981;&#x5FC5;&#x987B;&#x91CD;&#x8F7D;&#x64CD;&#x4F5C;&#x7B26;==()&#x3002;&#x6211;&#x662F;&#x8BF4;&#x5728;&#x4F60;&#x521B;&#x5EFA;&#x503C;&#x7C7B;&#x578B;&#x65F6;&#x624D;&#x91CD;&#x8F7D;&#x64CD;&#x4F5C;&#x7B26;==()&#x3002;.Net&#x6846;&#x67B6;&#x91CC;&#x7684;&#x7C7B;&#x8FD8;&#x662F;&#x671F;&#x671B;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x7684;==&#x64CD;&#x4F5C;&#x7B26;&#x8FD8;&#x662F;&#x4FDD;&#x7559;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x7684;&#x8BED;&#x4E49;&#x3002;<br/>C&#xFF03;&#x7ED9;&#x4E86;&#x4F60;4&#x79CD;&#x65B9;&#x6CD5;&#x6765;&#x68C0;&#x6D4B;&#x76F8;&#x7B49;&#x6027;&#xFF0C;&#x4F46;&#x4F60;&#x53EA;&#x987B;&#x8981;&#x8003;&#x8651;&#x4E3A;&#x5176;&#x4E2D;&#x4E24;&#x4E2A;&#x63D0;&#x4F9B;&#x4F60;&#x81EA;&#x5DF1;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x4F60;&#x51B3;&#x4E0D;&#x5E94;&#x8BE5;&#x91CD;&#x8F7D;&#x9759;&#x6001;&#x7684;Object.ReferenceEquals()&#x548C;&#x9759;&#x6001;&#x7684;Object.Equals()&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;&#x6B63;&#x786E;&#x7684;&#x68C0;&#x6D4B;&#xFF0C;&#x5FFD;&#x7565;&#x8FD0;&#x884C;&#x65F6;&#x7C7B;&#x578B;&#x3002;&#x4F60;&#x5E94;&#x8BE5;&#x4E3A;&#x4E86;&#x66F4;&#x597D;&#x7684;&#x6027;&#x80FD;&#x800C;&#x603B;&#x662F;&#x4E3A;&#x503C;&#x7C7B;&#x578B;&#x5B9E;&#x4F8B;&#x63D0;&#x4F9B;&#x91CD;&#x8F7D;&#x7684;Equals()&#x65B9;&#x6CD5;&#x548C;&#x64CD;&#x4F5C;&#x7B26;==()&#x3002;&#x5F53;&#x4F60;&#x5E0C;&#x671B;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x7684;&#x76F8;&#x7B49;&#x4E0E;&#x5BF9;&#x8C61;ID&#x7684;&#x76F8;&#x7B49;&#x4E0D;&#x540C;&#x65F6;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x91CD;&#x8F7D;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x5B9E;&#x4F8B;&#x7684;Equals()&#x3002;&#x7B80;&#x5355;&#xFF0C;&#x4E0D;&#x662F;&#x5417;&#xFF1F;<br/>========================================================<br/>Item 9: Understand the Relationships Among ReferenceEquals(), static Equals(), instance Equals(), and operator==<br/>When you cr&#101;ate your own types (either classes o&#114; structs), you define what equality means for that type. C# provides four different functions that determine whether two different objects are &#34;equal&#34;:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public static bool ReferenceEquals<br/>&nbsp;&nbsp;( object left, object right );<br/>public static bool Equals<br/>&nbsp;&nbsp;( object left, object right );<br/>public virtual bool Equals( object right);<br/>public static bool operator==( MyClass left, MyClass right );<br/>The language enables you to cr&#101;ate your own versions of all four of these methods. But just because you can doesn&#39;t mean that you should. You should never redefine the first two static functions. You&#39;ll often cr&#101;ate your own instance Equals() method to define the semantics of your type, and you&#39;ll occasionally override operator==(), but only for performance reasons in value types. Furthermore, there are relationships among these four functions, so when you change one, you can affect the behavior of the others. Yes, needing four functions to test equality is complicated. But don&#39;t worryyou can simplify it.<br/>Like so many of the complicated elements in C#, this one follows from the fact that C# enables you to cr&#101;ate both value types and reference types. Two variables of a reference type are equal if they refer to the sameobject, referred to as object identity. Two variables of a value type are equal if they are the same type and they contain the same contents. That&#39;s why equality tests need so many different methods.<br/>Let&#39;s start with the two functions you should never change. Object.ReferenceEquals() returns TRue if two variables refer to the same objectthat is, the two variables have the same object identity. Whether the types being compared are reference types o&#114; value types, this method always tests object identity, not object contents. Yes, that means that ReferenceEquals() always returns false when you use it to test equality for value types. Even when you compare a value type to itself, ReferenceEquals() returns false. This is due to boxing, which is covered in Item 16.<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;int i = 5;<br/>int j = 5;<br/>if ( Object.ReferenceEquals( i, j ))<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Never happens.&#34; );<br/>else<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Always happens.&#34; );<br/>if ( Object.ReferenceEquals( i, i ))<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Never happens.&#34; );<br/>else<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Always happens.&#34; );<br/>You&#39;ll never redefine Object.ReferenceEquals() becauseit does exactly what it is supposed to do: test the object identity of two different variables.<br/>The second function you&#39;ll never redefine is static Object.Equals(). This method tests whether two variables are equal when you don&#39;t know the runtime type of the two arguments. Remember that System.Object is the ultimate base class for everything in C#. Anytime you compare two variables, they are instances of System.Object. Value types and reference types are instances of System.Object. So how does this method test the equality of two variables, without knowing their type, when equality changes its meaning depending on the type? The answer is simple: This method delegates that responsibility to one of the types in question. The static Object.Equals() method is implemented something like this:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public static bool Equals( object left, object right )<br/>{<br/>&nbsp;&nbsp;// Check object identity<br/>&nbsp;&nbsp;if (left == right )<br/>&nbsp;&nbsp;&nbsp;&nbsp;return true;<br/>&nbsp;&nbsp;// both null references handled above<br/>&nbsp;&nbsp;if ((left == null) || (right == null))<br/>&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;return left.Equals (right);<br/>}<br/>This example code introduces both of the methods I have not discussed yet: operator==() and the instance Equals() method. I&#39;ll explain both in detail, but I&#39;m not ready to end my discussion of the static Equals() just yet. For right now, I want you to understand that static Equals() uses the instance Equals() method of the left argument to determine whether two objects are equal.<br/>As with ReferenceEquals(), you&#39;ll never redefine the static Object.Equals() method because it already does exactly what it needs to do: determines whether two objects are the same when you don&#39;t know the runtime type. Because the static Equals() method delegates to the left argument&#39;s instance Equals(), it uses the rules for that type.<br/>Now you understand why you never need to redefine the static ReferenceEquals() and static Equals() methods. It&#39;s time to discuss the methods you will override. But first, let&#39;s briefly discuss the mathematical properties of an equality relation. You need to make sure that your definition and implementation are consistent with other programmers&#39; expectations. This means that you need to keep in mind the mathematical properties of equality: Equality is reflexive, symmetric, and transitive. The reflexive property means that any object is equal to itself. No matter what type is involved, a == a is always true. The symmetric property means that o&#114;der does not matter: If a == b is true, b == a is also true. If a == b is false, b == a is also false. The last property is that if a == b and b == c are both true, then a == c must also be true. That&#39;s the transitive property.<br/>Now it&#39;s time to discuss the instance Object.Equals() function, including when and how you override it. You cr&#101;ate your own instance version of Equals() when the default behavior is inconsistent with your type. The Object.Equals() method uses object identity to determine whether two variables are equal. The default Object.Equals() function behaves exactly the same as Object.ReferenceEquals(). But waitvalue types are different. System.ValueType does override Object.Equals(). Remember that ValueType is the base class for all value types that you cr&#101;ate (using the struct keyword). Two variables of a value type are equal if they are the same type and they have the same contents. ValueType.Equals() implements that behavior. Unfortunately, ValueType.Equals() does not have an efficient implementation. ValueType.Equals() is the base class for all value types. To provide the correct behavior, it must compare all the member variables in any derived type, without knowing the runtime type of the object. In C#, that means using reflection. As you&#39;ll see in Item 44, there are many disadvantages to reflection, especially when performance is a goal. Equality is one of those fundamental constructs that gets called frequently in programs, so performance is a worthy goal. Under almost all circumstances, you can write a much faster override of Equals() for any value type. The recommendation for value types is simple: Always cr&#101;ate an override of ValueType.Equals() whenever you cr&#101;ate a value type.<br/>You should override the instance Equals() function only when you want to change the defined semantics for a reference type. A number of classes in the .NET Framework Class Library use value semantics instead of reference semantics for equality. Two string objects are equal if they contain the same contents. Two DataRowView objects are equal if they refer to the same DataRow. The point is that if your type should follow value semantics (comparing contents) instead of reference semantics (comparing object identity), you should write your own override of instance Object.Equals().<br/>Now that you know when to write your own override of Object.Equals(), you must understand how you should implement it. The equality relationship for value types has many implications for boxing and is discussed in Item 17. For reference types, your instance method needs to follow predefined behavior to avoid strange surprises for users of your class. Here is the standard pattern:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class Foo<br/>{<br/>&nbsp;&nbsp;public override bool Equals( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;// check null:<br/>&nbsp;&nbsp;&nbsp;&nbsp;// the this pointer is never null in C# methods.<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (right == null)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (object.ReferenceEquals( this, right ))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Discussed below.<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (this.GetType() != right.GetType())<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Compare this type&#39;s contents here:<br/>&nbsp;&nbsp;&nbsp;&nbsp;return CompareFooMembers(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this, right as Foo );<br/>&nbsp;&nbsp;}<br/>}<br/>First, Equals() should never throw exceptionsit doesn&#39;t make much sense. Two variables are o&#114; are not equal; there&#39;s not much room for other failures. Just return false for all failure conditions, such as null references o&#114; the wrong argument types. Now, let&#39;s go through this method in detail so you understand why each check is there and why some checks can be left out. The first check determines whether the right-side object is null. There is no check on this reference. In C#, this is never null. The CLR throws an exception before calling any instance method through a null reference. The next check determines whether the two object references are the same, testing object identity. It&#39;s a very efficient test, and equal object identity guarantees equal contents.<br/>The next check determines whether the two objects being compared are the same type. The exact form is important. First, notice that it does not assume that this is of type Foo; it calls this.GetType(). The actual type might be a class derived from Foo. Second, the code checks the exact type of objects being compared. It is not enough to ensure that you can convert the right-side parameter to the current type. That test can cause two subtle bugs. Consider the following example involving a small inheritance hierarchy:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class B<br/>{<br/>&nbsp;&nbsp;public override bool Equals( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;// check null:<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (right == null)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Check reference equality:<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (object.ReferenceEquals( this, right ))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Problems here, discussed below.<br/>&nbsp;&nbsp;&nbsp;&nbsp;B rightAsB = right as B;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (rightAsB == null)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return CompareBMembers( this, rightAsB );<br/>&nbsp;&nbsp;}<br/>}<br/>public class D : B<br/>{<br/>&nbsp;&nbsp;// etc.<br/>&nbsp;&nbsp;public override bool Equals( object right )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;// check null:<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (right == null)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (object.ReferenceEquals( this, right ))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// Problems here.<br/>&nbsp;&nbsp;&nbsp;&nbsp;D rightAsD = right as D;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (rightAsD == null)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (base.Equals( rightAsD ) == false)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return CompareDMembers( this, rightAsD );<br/>&nbsp;&nbsp;}<br/>}<br/>//Test:<br/>B baseObject = new B();<br/>D derivedObject = new D();<br/>// Comparison 1.<br/>if (baseObject.Equals(derivedObject))<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Equals&#34; );<br/>else<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Not Equal&#34; );<br/>// Comparison 2.<br/>if (derivedObject.Equals(baseObject))<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Equals&#34; );<br/>else<br/>&nbsp;&nbsp;Console.WriteLine( &#34;Not Equal&#34; );<br/>Under any possible circumstances, you would expect to see either Equals o&#114; Not Equal printed twice. Because of some errors, this is not the case with the previous code. The second comparison will never return TRue. The base object, of type B, can never be converted into a D. However, the first comparison might evaluate to true. The derived object, of type D, can be implicitly converted to a type B. If the B members of the right-side argument match the B members of the left-side argument, B.Equals() considers the objects equal. Even though the two objects are different types, your method has considered them equal. You&#39;ve broken the symmetric property of Equals. This construct broke because of the automatic conversions that take place up and down the inheritance hierarchy.<br/>When you write this, the D object is explicitly converted to a B:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;baseObject.Equals( derived )<br/>If baseObject.Equals() determines that the fields defined in its type match, the two objects are equal. On the other hand, when you write this, the B object cannot be converted to a D object:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;derivedObject.Equals( base )<br/>The B object cannot be converted to a D object. The derivedObject.Equals() method always returns false. If you don&#39;t check the object types exactly, you can easily get into this situation, in which the o&#114;der of the comparison matters.<br/>There is another practice to follow when you override Equals(). You should call the base class only if the base version is not provided by System.Object o&#114; System.ValueType. The previous code provides an example. Class D calls the Equals() method defined in its base class, Class B. However, Class B does not call baseObject.Equals(). It calls the version defined in System.Object, which returns true only when the two arguments refer to the same object. That&#39;s not what you want, o&#114; you wouldn&#39;t have written your own method in the first place.<br/>The rule is to override Equals() whenever you cr&#101;ate a value type, and to override Equals() on reference types when you do not want your reference type to obey reference semantics, as defined by System.Object. When you write your own Equals(), follow the implementation just outlined. Overriding Equals() means that you should write an override for GetHashCode(). See Item 10 for details.<br/>Three down, one to go: operator==(). Anytime you cr&#101;ate a value type, redefine operator==(). The reason is exactly the same as with the instance Equals() function. The default version uses reflection to compare the contents of two value types. That&#39;s far less efficient than any implementation that you would write, so write your own. Follow the recommendations in Item 17 to avoid boxing when you compare value types.<br/>Notice that I didn&#39;t say that you should write operator==() whenever you override instance Equals(). I said to write operator==() when you cr&#101;ate value types. You should rarely override operator==()when you cr&#101;ate reference types. The .NET Framework classes expect operator==() to follow reference semantics for all reference types.<br/>C# gives you four ways to test equality, but you need to consider providing your own definitions for only two of them. You never override the static Object.ReferenceEquals() and static Object.Equals() because they provide the correct tests, regardless of the runtime type. You always override instance Equals() and operator==() for value types to provide better performance. You override instance Equals() for reference types when you want equality to mean something other than object identity. Simple, right?</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.250&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;&#x39;&#xFF1A;&#x660E;&#x767D;&#x51E0;&#x4E2A;&#x76F8;&#x7B49;&#x8FD0;&#x7B97;&#x4E4B;&#x95F4;&#x7684;&#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.250&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.250&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card>
</wml>
