
<?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;&#x31;&#x30;&#x3A;&#x20;&#x660E;&#x767D;&#x47;&#x65;&#x74;&#x48;&#x61;&#x73;&#x68;&#x43;&#x6F;&#x64;&#x65;&#x28;&#x29;&#x7684;&#x7F3A;&#x9677;</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;&#x38;&#x20;&#x31;&#x30;&#x3A;&#x31;&#x32;&#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> &#x8FD9;&#x662F;&#x672C;&#x4E66;&#x4E2D;&#x552F;&#x4E00;&#x4E00;&#x4E2A;&#x88AB;&#x4E00;&#x6574;&#x4E2A;&#x51FD;&#x6570;&#x5360;&#x7528;&#x7684;&#x539F;&#x5219;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x907F;&#x514D;&#x5199;&#x8FD9;&#x6837;&#x7684;&#x51FD;&#x6570;&#x3002;GetHashCode()&#x4EC5;&#x5728;&#x4E00;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#x4F7F;&#x7528;&#xFF1A;&#x90A3;&#x5C31;&#x662F;&#x5BF9;&#x8C61;&#x88AB;&#x7528;&#x4E8E;&#x57FA;&#x4E8E;&#x6563;&#x5217;&#x7684;&#x96C6;&#x5408;&#x7684;&#x5173;&#x952E;&#x8BCD;&#xFF0C;&#x5982;&#x7ECF;&#x5178;&#x7684;HashTable&#x6216;&#x8005;Dictionary&#x5BB9;&#x5668;&#x3002;&#x8FD9;&#x5F88;&#x4E0D;&#x9519;&#xFF0C;&#x7531;&#x4E8E;&#x5728;&#x57FA;&#x7C7B;&#x4E0A;&#x5B9E;&#x73B0;&#x7684;GetHashCode()&#x5B58;&#x5728;&#x5927;&#x91CF;&#x7684;&#x95EE;&#x9898;&#x3002;&#x5BF9;&#x4E8E;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x5DE5;&#x4F5C;&#xFF0C;&#x4F46;&#x9AD8;&#x6548;&#x4E0D;&#x9AD8;&#xFF1B;&#x5BF9;&#x4E8E;&#x503C;&#x7C7B;&#x578B;&#xFF0C;&#x57FA;&#x7C7B;&#x7684;&#x5B9E;&#x73B0;&#x7ECF;&#x5E38;&#x51FA;&#x9519;&#x3002;&#x8FD9;&#x66F4;&#x7CDF;&#x7CD5;&#x3002;&#x4F60;&#x81EA;&#x5DF1;&#x5B8C;&#x5168;&#x53EF;&#x4EE5;&#x5199;&#x4E00;&#x4E2A;&#x5373;&#x9AD8;&#x6548;&#x53C8;&#x6B63;&#x786E;&#x7684;GetHashCode()&#x3002;&#x6CA1;&#x6709;&#x90A3;&#x4E2A;&#x5355;&#x4E00;&#x7684;&#x51FD;&#x6570;&#x6BD4;GetHashCode()&#x8BA8;&#x8BBA;&#x7684;&#x66F4;&#x591A;&#xFF0C;&#x4E14;&#x4EE4;&#x4EBA;&#x56F0;&#x60D1;&#x3002;&#x5F80;&#x4E0B;&#x770B;&#xFF0C;&#x4E3A;&#x4F60;&#x89E3;&#x91CA;&#x56F0;&#x60D1;&#x3002;<br/>&#x5982;&#x679C;&#x4F60;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#xFF0C;&#x800C;&#x4E14;&#x4F60;&#x51B3;&#x4E0D;&#x51C6;&#x5907;&#x628A;&#x5B83;&#x7528;&#x4E8E;&#x67D0;&#x4E2A;&#x5BB9;&#x5668;&#x7684;&#x5173;&#x952E;&#x8BCD;&#xFF0C;&#x90A3;&#x5C31;&#x6CA1;&#x4EC0;&#x4E48;&#x4E8B;&#x4E86;&#x3002;&#x50CF;&#x7A97;&#x4F53;&#x63A7;&#x4EF6;&#xFF0C;&#x7F51;&#x9875;&#x63A7;&#x4EF6;&#xFF0C;&#x6216;&#x8005;&#x6570;&#x636E;&#x5E93;&#x94FE;&#x63A5;&#x8FD9;&#x6837;&#x7684;&#x7C7B;&#x578B;&#x662F;&#x4E0D;&#x600E;&#x50CF;&#x8981;&#x505A;&#x4E3A;&#x67D0;&#x4E2A;&#x4EFB;&#x4F55;&#x7684;&#x5173;&#x952E;&#x8BCD;&#x7684;&#x3002;&#x5728;&#x8FD9;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4EC0;&#x4E48;&#x90FD;&#x4E0D;&#x7528;&#x505A;&#x4E86;&#x3002;&#x6240;&#x6709;&#x7684;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x90FD;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x6B63;&#x786E;&#x7684;&#x6563;&#x5217;&#x503C;&#xFF0C;&#x5373;&#x4F7F;&#x8FD9;&#x6837;&#x6548;&#x7387;&#x5F88;&#x7CDF;&#x7CD5;&#x3002;&#x503C;&#x7C7B;&#x578B;&#x5E94;&#x8BE5;&#x662F;&#x6052;&#x5B9A;&#x7684;(&#x53C2;&#x89C1;&#x539F;&#x5219;7)&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x9ED8;&#x8BA4;&#x7684;&#x5B9E;&#x73B0;&#x603B;&#x662F;&#x5DE5;&#x4F5C;&#x7684;&#xFF0C;&#x5C3D;&#x7BA1;&#x8FD9;&#x6837;&#x7684;&#x6548;&#x7387;&#x4E5F;&#x662F;&#x5F88;&#x7CDF;&#x7CD5;&#x7684;&#x3002;&#x5728;&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4F60;&#x6700;&#x597D;&#x5B8C;&#x5168;&#x907F;&#x514D;&#x5728;&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x4E0A;&#x4F7F;&#x7528;GetHashCode()&#x3002;<br/>&#x7136;&#x800C;&#xFF0C;&#x5728;&#x67D0;&#x5929;&#x4F60;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x8981;&#x505A;&#x4E3A;HashTable&#x7684;&#x5173;&#x952E;&#x8BCD;&#x6765;&#x4F7F;&#x7528;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x90A3;&#x4E48;&#x4F60;&#x5C31;&#x987B;&#x8981;&#x91CD;&#x5199;&#x4F60;&#x81EA;&#x5DF1;&#x7684;GetHashCode()&#x7684;&#x5B9E;&#x73B0;&#x4E86;&#x3002;&#x7EE7;&#x7EED;&#x770B;&#xFF0C;&#x57FA;&#x4E8E;&#x6563;&#x5217;(&#x7B97;&#x6CD5;)&#x7684;&#x96C6;&#x5408;&#x7528;&#x6563;&#x5217;&#x503C;&#x6765;&#x4F18;&#x5316;&#x67E5;&#x627E;&#x3002;&#x6BCF;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x6574;&#x578B;&#x7684;&#x6563;&#x5217;&#x503C;&#xFF0C;&#x800C;&#x8BE5;&#x5BF9;&#x8C61;&#x5C31;&#x5B58;&#x50A8;&#x5728;&#x57FA;&#x4E8E;&#x8FD9;&#x4E2A;&#x6563;&#x5217;&#x503C;&#x7684;&#x201C;&#x6876;&#x201D;&#x4E2D;&#x3002;&#x4E3A;&#x4E86;&#x67E5;&#x627E;&#x67D0;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x4F60;&#x901A;&#x8FC7;&#x5B83;&#x7684;&#x6563;&#x5217;&#x503C;&#x6765;&#x627E;&#x5230;&#x8FD9;&#x4E2A;(&#x5B58;&#x50A8;&#x4E86;&#x5B9E;&#x9645;&#x5BF9;&#x8C61;&#x7684;)&#x201C;&#x6876;&#x201D;&#x3002;&#x5728;.Net&#x91CC;&#xFF0C;&#x6BCF;&#x4E00;&#x5BF9;&#x8C61;&#x90FD;&#x6709;&#x4E00;&#x4E2A;&#x6563;&#x5217;&#x503C;&#xFF0C;&#x5B83;&#x662F;&#x7531;System.Object.GetHashCode()&#x65AD;&#x5B9A;&#x7684;&#x3002;&#x4EFB;&#x4F55;&#x5BF9;GetHashCode()&#x7684;&#x91CD;&#x5199;&#x90FD;&#x5FC5;&#x987B;&#x9075;&#x5B88;&#x4E0B;&#x9762;&#x7684;&#x4E09;&#x4E2A;&#x89C4;&#x5219;&#xFF1A;<br/>1&#x3001;&#x5982;&#x679C;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x76F8;&#x7B49;&#x7684;(&#x7531;&#x64CD;&#x4F5C;&#x7B26;==&#x6240;&#x5B9A;&#x4E49;)&#xFF0C;&#x90A3;&#x4E48;&#x5B83;&#x4EEC;&#x5FC5;&#x987B;&#x4EA7;&#x751F;&#x76F8;&#x540C;&#x7684;&#x6563;&#x5217;&#x503C;&#x3002;&#x5426;&#x5219;&#xFF0C;&#x65E0;&#x6CD5;&#x901A;&#x8FC7;&#x6563;&#x5217;&#x503C;&#x5728;&#x5BB9;&#x5668;&#x4E2D;&#x627E;&#x5230;&#x5BF9;&#x8C61;&#x3002;<br/>2&#x3001;&#x5BF9;&#x4E8E;&#x4EFB;&#x610F;&#x5BF9;&#x8C61;A&#xFF0C;A.GetHashCode()&#x5FC5;&#x987B;&#x662F;&#x5B9E;&#x4F8B;&#x4E0D;&#x53D8;&#x7684;&#x3002;&#x4E0D;&#x7BA1;&#x5728;A&#x4E0A;&#x8C03;&#x7528;&#x4E86;&#x4EC0;&#x4E48;&#x65B9;&#x6CD5;&#xFF0C;A.GetHashCode()&#x5FC5;&#x987B;&#x603B;&#x662F;&#x8FD4;&#x56DE;&#x540C;&#x6837;&#x7684;&#x6563;&#x5217;&#x503C;&#x3002;&#x8FD9;&#x5C31;&#x4FDD;&#x8BC1;&#x5728;&#x67D0;&#x4E2A;&#x201C;&#x6876;&#x201D;&#x4E2D;&#x7684;&#x5BF9;&#x8C61;&#x59CB;&#x7EC8;&#x662F;&#x5728;&#x8FD9;&#x4E2A;&#x201C;&#x6876;&#x201D;&#x4E2D;&#x3002;<br/>3&#x3001;&#x5BF9;&#x4E8E;&#x4EFB;&#x610F;&#x7684;&#x8F93;&#x5165;&#xFF0C;&#x6563;&#x5217;&#x51FD;&#x6570;&#x603B;&#x662F;&#x4EA7;&#x751F;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x4ECB;&#x4E8E;&#x6574;&#x578B;&#x5185;&#x7684;&#x968F;&#x673A;&#x5206;&#x5E03;&#x3002;&#x8FD9;&#x4F1A;&#x8BA9;&#x4F60;&#x5728;&#x4E00;&#x4E2A;&#x57FA;&#x4E8E;&#x6563;&#x5217;&#x7684;&#x5BB9;&#x5668;&#x53D6;&#x5F97;&#x597D;&#x7684;&#x6548;&#x7387;&#x3002;<br/>&#x4E3A;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x5199;&#x4E00;&#x4E2A;&#x6B63;&#x786E;&#x4E14;&#x9AD8;&#x6548;&#x7684;&#x6563;&#x5217;&#x51FD;&#x6570;&#x6765;&#x6EE1;&#x8DB3;&#x4E0A;&#x9762;&#x7684;&#x4E09;&#x6761;&#xFF0C;&#x8981;&#x5BF9;&#x8BE5;&#x7C7B;&#x578B;&#x6709;&#x5E7F;&#x6CDB;&#x7684;&#x8BA4;&#x8BC6;&#x3002;System.Object&#x548C;System.ValueType&#x7684;&#x9ED8;&#x8BA4;&#x7248;&#x672C;&#x5E76;&#x4E0D;&#x5177;&#x5907;&#x4EC0;&#x4E48;&#x4F18;&#x52BF;&#x3002;&#x8FD9;&#x4E9B;&#x7248;&#x672C;&#x5FC5;&#x987B;&#x4E3A;&#x4F60;&#x7684;&#x7279;&#x6B8A;&#x7C7B;&#x578B;&#x63D0;&#x4F9B;&#x9ED8;&#x8BA4;&#x7684;&#x884C;&#x4E3A;&#xFF0C;&#x800C;&#x540C;&#x65F6;&#x5B83;&#x4EEC;&#x5BF9;&#x8FD9;&#x4E9B;&#x7279;&#x6B8A;&#x7684;&#x7C7B;&#x578B;&#x53C8;&#x5E76;&#x4E0D;&#x4E86;&#x89E3;&#x3002;Object.GetHashCode()&#x662F;&#x4F7F;&#x7528;System.Object&#x5185;&#x5728;&#x7684;&#x6210;&#x5458;&#x6765;&#x4EA7;&#x751F;&#x6563;&#x5217;&#x503C;&#x3002;&#x6BCF;&#x4E2A;&#x5BF9;&#x8C61;&#x5728;&#x4EA7;&#x751F;&#x65F6;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x552F;&#x4E00;&#x7684;&#x503C;&#x6765;&#x505A;&#x4E3A;&#x5BF9;&#x8C61;&#x5173;&#x952E;&#x8BCD;&#xFF0C;(&#x8FD9;&#x4E2A;&#x503C;)&#x4EE5;&#x6574;&#x578B;&#x6765;&#x5B58;&#x50A8;&#x3002;&#x8FD9;&#x4E9B;&#x5173;&#x952E;&#x8BCD;&#x4ECE;1&#x5F00;&#x59CB;&#xFF0C;&#x5728;&#x6BCF;&#x6B21;&#x6709;&#x4EFB;&#x4F55;&#x65B0;&#x7684;&#x5BF9;&#x8C61;&#x4EA7;&#x751F;&#x65F6;&#x9010;&#x6E10;&#x589E;&#x52A0;&#x3002;&#x5BF9;&#x8C61;&#x7684;ID&#x5B57;&#x6BB5;&#x5728;System.Object&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x8FDB;&#x884C;&#x8BBE;&#x7F6E;&#xFF0C;&#x5E76;&#x4E14;&#x4ECA;&#x540E;&#x518D;&#x4E5F;&#x4E0D;&#x80FD;&#x4FEE;&#x6539;&#x3002;Object.GetHashCode()&#x5C31;&#x662F;&#x628A;&#x8FD9;&#x4E2A;&#x503C;&#x5F53;&#x6210;&#x7ED9;&#x5B9A;&#x5BF9;&#x8C61;&#x7684;&#x6563;&#x5217;&#x503C;&#x6765;&#x8FD4;&#x56DE;&#x3002;<br/>(&#x8BD1;&#x6CE8;&#xFF1A;&#x6CE8;&#x610F;&#x8FD9;&#x91CC;&#x8BF4;&#x7684;&#x662F;&#x9ED8;&#x8BA4;&#x7684;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#xFF0C;&#x5176;&#x5B83;&#x60C5;&#x51B5;&#x5C31;&#x4E0D;&#x662F;&#x8FD9;&#x6837;&#x7684;&#x4E86;&#x3002;)<br/>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6839;&#x636E;&#x4E0A;&#x9762;&#x7684;&#x4E09;&#x4E2A;&#x539F;&#x5219;&#x6765;&#x9A8C;&#x8BC1;Object.GetHashCode()&#x3002;&#x5982;&#x679C;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x76F8;&#x7B49;&#x7684;&#xFF0C;Object.GetHashCode()&#x8FD4;&#x56DE;&#x540C;&#x6837;&#x7684;&#x6563;&#x5217;&#x503C;&#xFF0C;&#x9664;&#x975E;&#x4F60;&#x91CD;&#x5199;&#x4E86;&#x64CD;&#x4F5C;&#x7B26;==&#x3002;System.Object&#x8FD9;&#x4E2A;&#x7248;&#x672C;&#x7684;==&#x64CD;&#x4F5C;&#x7B26;&#x662F;&#x68C0;&#x6D4B;&#x5BF9;&#x8C61;&#x7684;ID&#x3002;GetHashCode()&#x8FD4;&#x56DE;&#x5BF9;&#x8C61;&#x5185;&#x90E8;&#x7684;ID&#x5B57;&#x6BB5;&#xFF0C;&#x5B83;&#x662F;&#x597D;&#x7684;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x63D0;&#x4F9B;&#x4E86;&#x81EA;&#x5DF1;&#x7684;==&#x7248;&#x672C;&#xFF0C;&#x4F60;&#x5C31;&#x5FC5;&#x987B;&#x540C;&#x65F6;&#x63D0;&#x4F9B;&#x4F60;&#x81EA;&#x5DF1;&#x7248;&#x672C;&#x7684;GetHashCode()&#xFF0C;&#x4ECE;&#x800C;&#x4FDD;&#x8BC1;&#x9075;&#x5B88;&#x4E86;&#x524D;&#x9762;&#x8BF4;&#x7684;&#x7B2C;&#x4E00;&#x6761;&#x89C4;&#x5219;&#x3002;&#x76F8;&#x7B49;&#x7684;&#x7EC6;&#x8282;&#x53C2;&#x89C1;&#x539F;&#x5219;9&#x3002;<br/>&#x7B2C;&#x4E8C;&#x6761;&#x89C4;&#x5219;&#x5DF2;&#x7ECF;&#x9075;&#x5B88;&#x4E86;&#xFF1A;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x521B;&#x5EFA;&#x540E;&#xFF0C;&#x5B83;&#x7684;&#x6563;&#x5217;&#x503C;&#x662F;&#x4E0D;&#x80FD;&#x6539;&#x53D8;&#x7684;&#x3002;<br/>&#x7B2C;&#x4E09;&#x6761;&#x89C4;&#x5219;&#xFF0C;&#x5BF9;&#x6240;&#x6709;&#x7684;&#x8F93;&#x5165;&#xFF0C;&#x5728;&#x6574;&#x578B;&#x5185;&#x8FDB;&#x884C;&#x968F;&#x673A;&#x5206;&#x5E03;&#xFF0C;&#x8FD9;&#x5E76;&#x6CA1;&#x6709;&#x88AB;&#x652F;&#x6301;&#x3002;&#x8FD9;&#x4E2A;&#x6570;&#x5B57;&#x5E8F;&#x5217;&#x5E76;&#x4E0D;&#x662F;&#x6574;&#x578B;&#x4E0A;&#x7684;&#x968F;&#x673A;&#x5206;&#x5E03;&#xFF0C;&#x9664;&#x975E;&#x4F60;&#x521B;&#x5EFA;&#x4E86;&#x5927;&#x91CF;&#x7684;&#x5BF9;&#x8C61;&#x3002;Object.GetHashCode()&#x6240;&#x4EA7;&#x751F;&#x7684;&#x6563;&#x5217;&#x503C;&#x4E3B;&#x8981;&#x96C6;&#x4E2D;&#x5728;&#x5C3D;&#x53EF;&#x80FD;&#x5C0F;&#x7684;&#x6574;&#x578B;&#x8303;&#x56F4;&#x5185;&#x3002;<br/>&#x8FD9;&#x5C31;&#x662F;&#x8BF4;&#x8FD9;&#x4E2A;Object.GetHashCode()&#x662F;&#x6B63;&#x786E;&#x7684;&#xFF0C;&#x4F46;&#x5E76;&#x4E0D;&#x9AD8;&#x6548;&#x3002;&#x5982;&#x679C;&#x4F60;&#x5728;&#x4F60;&#x5B9A;&#x4E49;&#x7684;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x4E0A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6563;&#x5217;&#x8868;&#xFF0C;&#x8FD9;&#x4E2A;&#x9ED8;&#x8BA4;&#x4ECE;System.Object&#x4E0A;&#x7EE7;&#x627F;&#x7684;&#x884C;&#x4E3A;&#x662F;&#x5DE5;&#x4F5C;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x6BD4;&#x8F83;&#x6162;&#x3002;&#x5F53;&#x4F60;&#x521B;&#x5EFA;&#x51C6;&#x5907;&#x7528;&#x4E8E;&#x6563;&#x5217;&#x5173;&#x952E;&#x8BCD;&#x7684;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x4E3A;&#x4F60;&#x7684;&#x7279;&#x6B8A;&#x7C7B;&#x578B;&#x91CD;&#x5199;GetHashCode()&#xFF0C;&#x4ECE;&#x800C;&#x63D0;&#x4F9B;&#x66F4;&#x597D;&#x7684;&#x5728;&#x6574;&#x578B;&#x8303;&#x56F4;&#x4E0A;&#x968F;&#x673A;&#x5206;&#x5E03;&#x7684;&#x6563;&#x5217;&#x503C;&#x3002;<br/>&#x5728;&#x8BB2;&#x89E3;&#x5982;&#x4F55;&#x91CD;&#x5199;&#x4F60;&#x81EA;&#x5DF1;&#x7684;GetHashCode()&#x4E4B;&#x524D;&#xFF0C;&#x8FD9;&#x4E00;&#x8282;&#x6765;&#x9A8C;&#x8BC1;ValueType.GetHashCode()&#x662F;&#x5426;&#x4E5F;&#x9075;&#x5B88;&#x4E0A;&#x9762;&#x7684;&#x4E09;&#x6761;&#x89C4;&#x5219;&#x3002;System.ValueType&#x91CD;&#x5199;&#x4E86;GetHashCode()&#xFF0C;&#x4E3A;&#x6240;&#x6709;&#x7684;&#x503C;&#x7C7B;&#x578B;&#x63D0;&#x4F9B;&#x9ED8;&#x8BA4;&#x7684;&#x884C;&#x4E3A;&#x3002;&#x8FD9;&#x4E00;&#x7248;&#x672C;&#x4ECE;&#x4F60;&#x6240;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x578B;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x4E0A;&#x8FD4;&#x56DE;&#x6563;&#x5217;&#x3002;&#x8003;&#x8651;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct MyStruct<br/>{<br/>&nbsp;&nbsp;private string&nbsp;&nbsp; _msg;<br/>&nbsp;&nbsp;private int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_id;<br/>&nbsp;&nbsp;private DateTime _epoch;<br/>}<br/>&#x4ECE;MyStruct&#x5BF9;&#x8C61;&#x4E0A;&#x8FD4;&#x56DE;&#x7684;&#x6563;&#x5217;&#x503C;&#x662F;&#x4ECE;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;_msg&#x6210;&#x5458;&#x4E0A;&#x751F;&#x6210;&#x7684;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x7247;&#x65AD;&#x603B;&#x662F;&#x8FD4;&#x56DE;true:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;MyStruct s = new MyStruct( );<br/>return s.GetHashCode( ) == s._msg.GetHashCode( );<br/>&#x89C4;&#x5219;1&#x8868;&#x793A;&#xFF0C;&#x4E24;&#x4E2A;&#x76F8;&#x7B49;&#x7684;&#x5BF9;&#x8C61;(&#x7528;&#x64CD;&#x4F5C;&#x7B26;==&#x5B9A;&#x4E49;&#x7684;)&#x5FC5;&#x987B;&#x8FD4;&#x56DE;&#x76F8;&#x540C;&#x7684;&#x6563;&#x5217;&#x503C;&#x3002;&#x8FD9;&#x4E00;&#x89C4;&#x5219;&#x88AB;&#x5927;&#x591A;&#x6570;&#x503C;&#x7C7B;&#x578B;&#x9075;&#x5B88;&#x7740;&#xFF0C;&#x4F46;&#x4F60;&#x53EF;&#x4EE5;&#x7834;&#x574F;&#x5B83;&#xFF0C; just as you could with for reference types. ValueType&#x7684;&#x64CD;&#x4F5C;&#x7B26;==()&#x4E0E;&#x5176;&#x5B83;&#x6210;&#x5458;&#x4E00;&#x8D77;&#x6765;&#x6BD4;&#x8F83;&#x7ED3;&#x6784;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x3002;&#x8FD9;&#x662F;&#x6EE1;&#x8DB3;&#x7B2C;&#x4E00;&#x6761;&#x89C4;&#x5219;&#x7684;&#x3002;&#x53EA;&#x8981;&#x5728;&#x4EFB;&#x4F55;&#x65F6;&#x5019;&#x4F60;&#x6240;&#x91CD;&#x5199;&#x7684;==&#x64CD;&#x4F5C;&#x7B26;&#x7528;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#xFF0C;&#x8FD9;&#x5C06;&#x53EF;&#x4EE5;&#x6B63;&#x5E38;&#x5DE5;&#x4F5C;&#x3002;&#x4EFB;&#x4F55;&#x4E0D;&#x4EE5;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x65AD;&#x5B9A;&#x76F8;&#x7B49;&#x7684;&#x7ED3;&#x6784;&#x5C06;&#x8FDD;&#x53CD;&#x8FD9;&#x4E00;&#x89C4;&#x5219;&#xFF0C;&#x4ECE;&#x800C;&#x7834;&#x574F;GetHashCode().<br/>&#x7B2C;&#x4E8C;&#x6761;&#x89C4;&#x5219;&#x8868;&#x793A;&#xFF0C;&#x6563;&#x5217;&#x503C;&#x5FC5;&#x987B;&#x662F;&#x5B9E;&#x4F8B;&#x4E0D;&#x53D8;&#x7684;&#x3002;&#x8FD9;&#x4E00;&#x89C4;&#x5219;&#x53EA;&#x6709;&#x5F53;&#x7ED3;&#x6784;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#x5B57;&#x6BB5;&#x662F;&#x6052;&#x5B9A;&#x7C7B;&#x578B;&#x65F6;&#x624D;&#x88AB;&#x9075;&#x5B88;&#x3002;&#x5982;&#x679C;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x7684;&#x503C;&#x53D1;&#x751F;&#x4E86;&#x6539;&#x53D8;&#xFF0C;&#x90A3;&#x4E48;&#x6563;&#x5217;&#x503C;&#x4E5F;&#x4F1A;&#x53D1;&#x751F;&#x6539;&#x53D8;&#x3002;&#x8FD9;&#x5C31;&#x7834;&#x574F;&#x4E86;&#x8FD9;&#x89C4;&#x5219;&#x3002;&#x662F;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x7684;&#x7ED3;&#x6784;&#x7684;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x5728;&#x5B83;&#x7684;&#x751F;&#x5B58;&#x671F;&#x5185;&#x6539;&#x53D8;&#x4E86;&#x7ED3;&#x6784;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#xFF0C;&#x90A3;&#x4E48;GetHashCode()&#x5C31;&#x7834;&#x574F;&#x4E86;&#x8FD9;&#x4E00;&#x89C4;&#x5219;&#x3002;&#x8FD9;&#x4E5F;&#x5C31;&#x662F;&#x53E6;&#x4E00;&#x4E2A;&#x539F;&#x56E0;&#xFF0C;&#x4F60;&#x6700;&#x597D;&#x7684;&#x539F;&#x5219;&#x5C31;&#x662F;&#x628A;&#x503C;&#x7C7B;&#x578B;&#x8BBE;&#x7F6E;&#x4E3A;&#x6052;&#x5B9A;&#x7C7B;&#x578B;(&#x53C2;&#x89C1;&#x539F;&#x5219;7)&#x3002;<br/>&#x7B2C;&#x4E09;&#x4E2A;&#x89C4;&#x5219;&#x4F9D;&#x61D2;&#x4E8E;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x4EE5;&#x53CA;&#x662F;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x5B83;&#x7684;&#x3002;&#x5982;&#x679C;&#x4F60;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x80FD;&#x4FDD;&#x8BC1;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x5728;&#x6574;&#x578B;&#x8303;&#x56F4;&#x4E0A;&#x7684;&#x968F;&#x673A;&#x5206;&#x5E03;&#xFF0C;&#x5E76;&#x4E14;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x7684;&#x5206;&#x5E03;&#x80FD;&#x590D;&#x76D6;&#x7ED3;&#x6784;&#x7684;&#x6240;&#x6709;&#x5176;&#x5B83;&#x503C;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x7ED3;&#x6784;&#x5C31;&#x5F88;&#x597D;&#x7684;&#x4FDD;&#x8BC1;&#x4E86;&#x4E00;&#x4E2A;&#x5747;&#x8861;&#x7684;&#x5206;&#x5E03;(&#x8BD1;&#x6CE8;&#xFF1A;&#x5C31;&#x662F;&#x8BF4;&#x7ED3;&#x6784;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x53EF;&#x4EE5;&#x552F;&#x4E00;&#x7684;&#x51B3;&#x5B9A;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;)&#x3002;&#x7136;&#x800C;&#xFF0C;&#x5982;&#x679C;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x7ECF;&#x5E38;&#x5177;&#x6709;&#x76F8;&#x540C;&#x7684;&#x503C;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E00;&#x89C4;&#x5219;&#x4E5F;&#x4F1A;&#x88AB;&#x7834;&#x574F;&#x3002;&#x8003;&#x8651;&#x5BF9;&#x524D;&#x9762;&#x7684;&#x7ED3;&#x6784;&#x505A;&#x4E00;&#x4E2A;&#x5C0F;&#x7684;&#x4FEE;&#x6539;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct MyStruct<br/>{<br/>&nbsp;&nbsp;private DateTime _epoch;<br/>&nbsp;&nbsp;private string&nbsp;&nbsp; _msg;<br/>&nbsp;&nbsp;private int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_id;<br/>}<br/>&#x5982;&#x679C;_epoch&#x5B57;&#x6BB5;&#x8BBE;&#x7F6E;&#x7684;&#x662F;&#x5F53;&#x524D;&#x65E5;&#x671F;(&#x4E0D;&#x5305;&#x542B;&#x65F6;&#x95F4;)&#xFF0C;&#x6240;&#x6709;&#x5728;&#x540C;&#x4E00;&#x7ED9;&#x5B9A;&#x65E5;&#x671F;&#x91CC;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x5177;&#x6709;&#x76F8;&#x540C;&#x7684;&#x6563;&#x5217;&#x503C;&#x3002;&#x8FD9;&#x9632;&#x788D;&#x4E86;&#x5728;&#x6240;&#x6709;&#x6563;&#x5217;&#x503C;&#x4E2D;&#x8FDB;&#x884C;&#x5747;&#x8861;&#x7684;&#x5206;&#x5E03;&#x3002;<br/>&#x6982;&#x62EC;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x7684;&#x884C;&#x4E3A;&#xFF0C;Object.GetHashCode()&#x53EF;&#x4EE5;&#x6B63;&#x786E;&#x7684;&#x5728;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x4E0A;&#x5DE5;&#x4F5C;&#xFF0C;&#x5C3D;&#x7BA1;&#x5B83;&#x4E0D;&#x662F;&#x5FC5;&#x987B;&#x4FDD;&#x8BC1;&#x4E00;&#x4E2A;&#x9AD8;&#x6548;&#x7684;&#x5206;&#x5E03;&#x3002;(&#x5982;&#x679C;&#x4F60;&#x6709;&#x4E00;&#x4E2A;&#x5BF9;Object&#x7684;==&#x64CD;&#x4F5C;&#x7B26;&#x7684;&#x91CD;&#x8F7D;&#xFF0C;&#x4F60;&#x4F1A;&#x7834;&#x574F;GetHashCode())&#x3002;ValueType.GetHashCode()&#x4EC5;&#x5728;&#x4F60;&#x7684;&#x7ED3;&#x6784;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x662F;&#x53EA;&#x8BFB;&#x7684;&#x65F6;&#x5019;&#x624D;&#x80FD;&#x6B63;&#x786E;&#x5DE5;&#x4F5C;&#x3002;&#x800C;&#x5F53;&#x4F60;&#x7684;&#x7ED3;&#x6784;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x7684;&#x503C;&#xFF0C;&#x590D;&#x76D6;&#x4E86;&#x5B83;&#x6240;&#x80FD;&#x63A5;&#x53D7;&#x7684;&#x8F93;&#x5165;&#x7684;&#x6709;&#x610F;&#x4E49;&#x7684;&#x5B50;&#x96C6;&#x65F6;&#xFF0C;ValueType.GetHashCode()&#x5C31;&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x4E00;&#x4E2A;&#x9AD8;&#x6548;&#x7684;&#x6563;&#x5217;&#x503C;&#x3002;<br/>&#x5982;&#x679C;&#x4F60;&#x51C6;&#x5907;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x66F4;&#x597D;&#x7684;&#x6563;&#x5217;&#x503C;&#xFF0C;&#x4F60;&#x987B;&#x8981;&#x4E3A;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x5EFA;&#x7ACB;&#x4E00;&#x4E9B;&#x7EA6;&#x675F;&#x3002;&#x518D;&#x6B21;&#x9A8C;&#x8BC1;&#x4E0A;&#x9762;&#x7684;&#x4E09;&#x6761;&#x89C4;&#x5219;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6765;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x53EF;&#x5DE5;&#x4F5C;&#x7684;GetHashCode()&#x3002;<br/>&#x9996;&#x5148;&#xFF0C;&#x5982;&#x679C;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x76F8;&#x7B49;&#xFF0C;&#x5C31;&#x662F;&#x7531;&#x64CD;&#x4F5C;&#x7B26;==&#x6240;&#x5B9A;&#x4E49;&#x7684;&#xFF0C;&#x5B83;&#x4EEC;&#x5FC5;&#x987B;&#x8FD4;&#x56DE;&#x540C;&#x6837;&#x7684;&#x6563;&#x5217;&#x503C;&#x3002;&#x7C7B;&#x578B;&#x7684;&#x4EFB;&#x4F55;&#x627F;&#x62C5;&#x6563;&#x5217;&#x503C;&#x7684;&#x5C5E;&#x6027;&#x6216;&#x8005;&#x6570;&#x636E;&#x503C;&#x4E5F;&#x5FC5;&#x987B;&#x53C2;&#x4E0E;&#x76F8;&#x7B49;&#x6BD4;&#x8F83;&#x3002;&#x663E;&#x7136;&#xFF0C;&#x8FD9;&#x5C31;&#x610F;&#x5473;&#x7740;&#x540C;&#x6837;&#x7528;&#x4E8E;&#x76F8;&#x7B49;&#x6BD4;&#x8F83;&#x7684;&#x7684;&#x5C5E;&#x6027;&#x4E5F;&#x7528;&#x4E8E;&#x6563;&#x5217;&#x503C;&#x7684;&#x751F;&#x6210;&#x3002;&#x7136;&#x800C;&#x5F88;&#x53EF;&#x80FD;&#x6240;&#x6709;&#x4E0E;&#x76F8;&#x7B49;&#x6BD4;&#x8F83;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x4E0D;&#x7528;&#x4E8E;&#x6563;&#x5217;&#x503C;&#x7684;&#x8BA1;&#x7B97;&#x3002;System.ValueType&#x7684;&#x9ED8;&#x8BA4;&#x884C;&#x4E3A;&#x5C31;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x5B83;&#x7ECF;&#x5E38;&#x8FDD;&#x53CD;&#x89C4;&#x5219;3&#x3002;&#x540C;&#x6837;&#x7684;&#x6570;&#x636E;&#x5143;&#x7D20;&#x5E94;&#x8BE5;&#x53C2;&#x540C;&#x65F6;&#x53C2;&#x4E0E;&#x4E24;&#x4E2A;&#x8FD0;&#x7B97;(&#x6BD4;&#x8F83;&#x548C;&#x6563;&#x5217;)&#x3002;<br/>&#x7B2C;&#x4E8C;&#x6761;&#x89C4;&#x5219;&#x5C31;&#x662F;GetHashCode()&#x8FD4;&#x56DE;&#x7684;&#x503C;&#x5FC5;&#x987B;&#x662F;&#x5B9E;&#x4F8B;&#x4E0D;&#x53D8;&#x7684;&#x3002;&#x60F3;&#x8C61;&#x4F60;&#x5DF2;&#x7ECF;&#x4E86;&#x4E00;&#x4E2A;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#xFF0C;Customer:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class Customer<br/>{<br/>&nbsp;&nbsp;private string _name;<br/>&nbsp;&nbsp;private decimal _revenue;<br/>&nbsp;&nbsp;public Customer( string name )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_name = name;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public string Name<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;get { return _name; }<br/>&nbsp;&nbsp;&nbsp;&nbsp;set { _name = value; }<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public override int GetHashCode()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return _name.GetHashCode();<br/>&nbsp;&nbsp;}<br/>}<br/>&#x5047;&#x8BBE;&#x4F60;&#x8FD0;&#x884C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x7247;&#x65AD;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Customer c1 = new Customer( &#34;Acme Products&#34; );<br/>myHashMap.Add( c1, o&#114;ders );<br/>// Oops, the name is wrong:<br/>c1.Name = &#34;Acme Software&#34;;<br/>c1&#x5728;&#x67D0;&#x4E9B;&#x5730;&#x65B9;&#x4F1A;&#x4E22;&#x5931;&#x6563;&#x5217;&#x6620;&#x5C04;&#x3002;&#x5F53;&#x4F60;&#x628A;c1&#x653E;&#x5728;&#x6620;&#x5C04;&#x4E2D;&#x65F6;&#xFF0C;&#x6563;&#x5217;&#x503C;&#x662F;&#x7531;&#x5B57;&#x7B26;&#x4E32;&#x201C;Acme Products&#x201D;&#x6765;&#x4FDD;&#x8BC1;&#x7684;&#x3002;&#x5F53;&#x4F60;&#x628A;&#x5BA2;&#x6237;&#x7684;&#x540D;&#x5B57;&#x4FEE;&#x6539;&#x4E3A;&#x201C;Acme Software&#x201D;&#x540E;&#xFF0C;&#x6563;&#x5217;&#x503C;&#x4E5F;&#x53D1;&#x751F;&#x4E86;&#x6539;&#x53D8;&#x3002;&#x73B0;&#x5728;&#x662F;&#x7531;&#x65B0;&#x7684;&#x540D;&#x5B57;&#xFF1A;&#x201C;Acme Software&#x201D;&#x6765;&#x4FDD;&#x8BC1;&#x7684;&#x4E86;&#x3002;c1&#x5B58;&#x50A8;&#x5728;&#x4E00;&#x4E2A;&#x7531;&#x201C;Acme Products&#x201D;&#x51B3;&#x5B9A;&#x7684;&#x201C;&#x6876;&#x201D;&#x5185;&#xFF0C;&#x800C;&#x5B83;&#x4E0D;&#x5E94;&#x8BE5;&#x5B58;&#x5728;&#x4E8E;&#x7531;&#x201C;Acme Software&#x201D;&#x51B3;&#x5B9A;&#x7684;&#x201C;&#x6876;&#x201D;&#x5185;&#x3002;&#x4F60;&#x5C06;&#x4F1A;&#x5728;&#x4F60;&#x81EA;&#x5DF1;&#x7684;&#x96C6;&#x5408;&#x4E2D;&#x4E22;&#x5931;&#x8FD9;&#x4E2A;&#x5BA2;&#x6237;&#x3002;&#x4E22;&#x5931;&#x7684;&#x539F;&#x56E0;&#x5C31;&#x662F;&#x6563;&#x5217;&#x503C;&#x4E0D;&#x662F;&#x5B9E;&#x4F8B;&#x4E0D;&#x53D8;&#x7684;&#x3002;&#x4F60;&#x5728;&#x5BF9;&#x8C61;&#x5B58;&#x50A8;&#x540E;&#xFF0C;&#x6539;&#x53D8;&#x4E86;&#x6B63;&#x786E;&#x7684;&#x201C;&#x6876;&#x201D;&#x3002;<br/>&#x524D;&#x9762;&#x7684;&#x60C5;&#x5F62;&#x53EA;&#x6709;&#x5F53;Customer&#x662F;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x65F6;&#x624D;&#x51FA;&#x73B0;&#x3002;&#x800C;&#x5F53;&#x662F;&#x503C;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x4F1A;&#x6709;&#x4E0D;&#x540C;&#x7684;&#x9519;&#x8BEF;&#x884C;&#x4E3A;&#xFF0C;&#x800C;&#x4E14;&#x540C;&#x6837;&#x662F;&#x5E26;&#x6765;&#x9EBB;&#x70E6;&#x7684;&#x3002;&#x5982;&#x679C;Customer&#x662F;&#x4E00;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#xFF0C;c1&#x7684;&#x4E00;&#x4E2A;&#x62F7;&#x8D1D;&#x4F1A;&#x5B58;&#x5728;&#x5728;&#x6563;&#x5217;&#x6620;&#x5C04;&#x4E2D;&#x3002;&#x56E0;&#x4E3A;&#x88C5;&#x7BB1;&#x4E0E;&#x62C6;&#x7BB1;&#x5F88;&#x4F1A;&#x62F7;&#x8D1D;&#x6570;&#x636E;&#x3002;&#x8FD9;&#x5E76;&#x4E0D;&#x662F;&#x5F88;&#x53EF;&#x9760;&#xFF0C;&#x5F53;&#x4F60;&#x5728;&#x628A;&#x4E00;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x5230;&#x96C6;&#x5408;&#x4E4B;&#x540E;&#xFF0C;&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x6210;&#x5458;&#x6570;&#x636E;&#x3002;<br/>&#x552F;&#x4E00;&#x5B89;&#x7F6E;&#x597D;&#x89C4;&#x5219;2&#x7684;&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#xFF0C;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6563;&#x5217;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x4F9D;&#x61D2;&#x4E8E;&#x5BF9;&#x8C61;&#x7684;&#x4E00;&#x4E9B;&#x4E0D;&#x53D8;&#x7684;&#x5C5E;&#x6027;&#x6765;&#x8FD4;&#x56DE;&#x6563;&#x5217;&#x503C;&#x3002;System.Object&#x662F;&#x901A;&#x8FC7;&#x4E0D;&#x53D8;&#x7684;&#x5BF9;&#x8C61;ID&#x6765;&#x9075;&#x5B88;&#x8FD9;&#x4E00;&#x89C4;&#x5219;&#x7684;&#x3002;System.ValueType&#x5E0C;&#x671B;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x4E0D;&#x53D1;&#x751F;&#x6539;&#x53D8;&#x3002;&#x9664;&#x4E86;&#x628A;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x8BBE;&#x8BA1;&#x4E3A;&#x6052;&#x5B9A;&#x7C7B;&#x578B;&#x4EE5;&#x5916;&#xFF0C;&#x4F60;&#x6CA1;&#x6709;&#x66F4;&#x597D;&#x7684;&#x65B9;&#x6CD5;&#x4E86;&#x3002;&#x5F53;&#x4F60;&#x51C6;&#x5907;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x5728;&#x6563;&#x5217;&#x5BB9;&#x5668;&#x4E2D;&#x5F53;&#x5173;&#x952E;&#x8BCD;&#x4F7F;&#x7528;&#x7684;&#x503C;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x5B83;&#x5FC5;&#x987B;&#x662F;&#x4E00;&#x4E2A;&#x6052;&#x5B9A;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x9075;&#x5B88;&#x529D;&#x544A;&#xFF0C;&#x4F60;&#x7684;&#x7528;&#x6237;&#x5C31;&#x53EF;&#x4EE5;&#x628A;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x505A;&#x4E3A;&#x4E00;&#x4E2A;&#x5173;&#x952E;&#x8BCD;&#x5728;&#x6563;&#x5217;&#x8868;&#x4E2D;&#x4F7F;&#x7528;&#xFF0C;&#x8FDB;&#x800C;&#x627E;&#x5230;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x7834;&#x574F;&#x6563;&#x5217;&#x8868;&#x3002; &#x66F4;&#x6B63;Customer&#x7C7B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x5B83;&#xFF0C;&#x4F7F;&#x5BA2;&#x6237;&#x540D;&#x6210;&#x4E3A;&#x4E00;&#x4E2A;&#x6052;&#x5B9A;&#x7684;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class Customer<br/>{<br/>&nbsp;&nbsp;private readonly string _name;<br/>&nbsp;&nbsp;private decimal _revenue;<br/>&nbsp;&nbsp;public Customer( string name ) :<br/>&nbsp;&nbsp;&nbsp;&nbsp;this ( name, 0 )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public Customer( string name, decimal revenue )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_name = name;<br/>&nbsp;&nbsp;&nbsp;&nbsp;_revenue = revenue;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public string Name<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;get { return _name; }<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;// Change the name, returning a new object:<br/>&nbsp;&nbsp;public Customer ChangeName( string newName )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return new Customer( newName, _revenue );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public override int GetHashCode()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return _name.GetHashCode();<br/>&nbsp;&nbsp;}<br/>}<br/>&#x4F7F;&#x540D;&#x5B57;&#x6210;&#x4E3A;&#x6052;&#x5B9A;&#x7684;&#x7C7B;&#x578B;&#x540E;&#xFF0C;&#x4F60;&#x8981;&#x600E;&#x6837;&#x624D;&#x80FD;&#x4FEE;&#x6539;&#x4E00;&#x4E2A;&#x5BA2;&#x6237;&#x5BF9;&#x8C61;&#x7684;&#x540D;&#x5B57;&#x5462;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Customer c1 = new Customer( &#34;Acme Products&#34; );<br/>myHashMap.Add( c1,orders );<br/>// Oops, the name is wrong:<br/>Customer c2 = c1.ChangeName( &#34;Acme Software&#34; );<br/>o&#114;der o = myHashMap[ c1 ] as o&#114;der;<br/>myHashMap.Remove( c1 );<br/>myHashMap.Add( c2, o );<br/>&#x4F60;&#x5DF2;&#x7ECF;&#x79FB;&#x9664;&#x4E86;&#x539F;&#x6765;&#x7684;&#x5BA2;&#x6237;&#xFF0C;&#x4FEE;&#x6539;&#x4E86;&#x540D;&#x5B57;&#xFF0C;&#x7136;&#x540E;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5BA2;&#x6237;&#x5BF9;&#x8C61;&#x5230;&#x6563;&#x5217;&#x8868;&#x4E2D;&#x3002;&#x8FD9;&#x770B;&#x4E0A;&#x53BB;&#x6BD4;&#x539F;&#x6765;&#x7684;&#x66F4;&#x9EBB;&#x70E6;&#xFF0C;&#x4F46;&#x5B83;&#x53EF;&#x4EE5;&#x6B63;&#x786E;&#x5DE5;&#x4F5C;&#x3002;&#x5148;&#x524D;&#x7684;&#x7248;&#x672C;&#x5145;&#x8BB8;&#x7A0B;&#x5E8F;&#x5458;&#x5199;&#x4E00;&#x4E9B;&#x4E0D;&#x6B63;&#x786E;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x901A;&#x8FC7;&#x5F3A;&#x5236;&#x4F7F;&#x7528;&#x6052;&#x5B9A;&#x5C5E;&#x6027;&#x6765;&#x751F;&#x6210;&#x6563;&#x5217;&#x503C;&#x540E;&#xFF0C;&#x4F60;&#x5C31;&#x589E;&#x52A0;&#x4E86;&#x6B63;&#x786E;&#x7684;&#x884C;&#x4E3A;&#x3002;&#x4F60;&#x7684;&#x7528;&#x6237;&#x5C31;&#x4E0D;&#x4F1A;&#x51FA;&#x9519;&#x4E86;&#x3002;&#x662F;&#x7684;&#xFF0C;&#x8FD9;&#x4E2A;&#x7248;&#x672C;&#x53EF;&#x4EE5;&#x66F4;&#x597D;&#x7684;&#x5DE5;&#x4F5C;&#x3002;&#x4F60;&#x8FEB;&#x4F7F;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x5199;&#x66F4;&#x591A;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x4F46;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x53EA;&#x6709;&#x8FD9;&#x6837;&#x624D;&#x80FD;&#x5199;&#x6B63;&#x786E;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x8BF7;&#x786E;&#x4FDD;&#x53C2;&#x4E0E;&#x6563;&#x5217;&#x8FD0;&#x7B97;&#x7684;&#x6570;&#x636E;&#x6210;&#x5458;&#x662F;&#x6052;&#x5B9A;&#x7684;&#x3002;<br/>&#x7B2C;&#x4E09;&#x6761;&#x89C4;&#x5219;&#x662F;&#x8BF4;&#xFF0C;GetHashCode()&#x5E94;&#x8BE5;&#x5BF9;&#x6240;&#x6709;&#x7684;&#x8F93;&#x5165;&#x968F;&#x673A;&#x7684;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x5206;&#x5E03;&#x5728;&#x6574;&#x578B;&#x8303;&#x56F4;&#x5185;&#x7684;&#x503C;&#x3002;&#x8FD9;&#x4E00;&#x9700;&#x6C42;&#x4F9D;&#x61D2;&#x4E8E;&#x4F60;&#x5B9E;&#x9645;&#x521B;&#x5EFA;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x5982;&#x679C;&#x6709;&#x4E00;&#x4E2A;&#x795E;&#x5947;&#x7684;&#x516C;&#x5F0F;&#x5B58;&#x5728;&#xFF0C;&#x90A3;&#x5B83;&#x5E94;&#x8BE5;&#x5728;System.Object&#x4E2D;&#x5B9E;&#x73B0;&#x4E86;&#xFF0C;&#x5E76;&#x4E14;&#x8FD9;&#x4E00;&#x539F;&#x5219;(&#x8BD1;&#x6CE8;&#xFF1A;&#x8FD9;&#x91CC;&#x8BF4;&#x7684;&#x662F;&#x5168;&#x6587;&#x8FD9;&#x4E00;&#x539F;&#x5219;)&#x4E5F;&#x5C06;&#x4E0D;&#x5B58;&#x5728;&#x4E86;&#x3002;&#x4E00;&#x4E2A;&#x901A;&#x7528;&#x800C;&#x4E14;&#x6210;&#x529F;&#x7684;&#x7B97;&#x6CD5;&#x5C31;&#x662F;XOR(&#x5F02;&#x6216;)&#x8FD0;&#x7B97;&#xFF0C;&#x5BF9;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x5185;&#x7684;&#x6240;&#x6709;&#x5B57;&#x6BB5;&#x7684;&#x6563;&#x5217;&#x518D;&#x8FDB;&#x884C;&#x5F02;&#x6216;&#x540E;&#x8FD4;&#x56DE;&#x3002;&#x5982;&#x679C;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x91CC;&#x5305;&#x542B;&#x4E00;&#x4E9B;&#x6301;&#x4E45;&#x5B57;&#x6BB5;&#xFF0C;&#x8BA1;&#x7B97;&#x65F6;&#x5E94;&#x8BE5;&#x6392;&#x9664;&#x5B83;&#x4EEC;&#x3002;<br/>GetHashCode()&#x5177;&#x6709;&#x5F88;&#x7279;&#x6B8A;&#x7684;&#x8981;&#x6C42;&#xFF1A;&#x76F8;&#x7B49;&#x7684;&#x5BF9;&#x8C61;&#x5FC5;&#x987B;&#x4EA7;&#x751F;&#x76F8;&#x7B49;&#x7684;&#x6563;&#x5217;&#x503C;&#xFF0C;&#x5E76;&#x4E14;&#x6563;&#x5217;&#x503C;&#x5FC5;&#x987B;&#x662F;&#x5BF9;&#x8C61;&#x4E0D;&#x53D8;&#x7684;&#xFF0C;&#x5E76;&#x4E14;&#x662F;&#x5747;&#x8861;&#x7684;&#x9AD8;&#x6548;&#x5206;&#x5E03;&#x3002;&#x6240;&#x6709;&#x8FD9;&#x4E9B;&#x53EA;&#x6709;&#x5BF9;&#x6052;&#x5B9A;&#x7C7B;&#x578B;&#x624D;&#x80FD;&#x6EE1;&#x8DB3;(&#x8BD1;&#x6CE8;&#xFF1A;&#x672C;&#x6587;&#x524D;&#x9762;&#x5DF2;&#x7ECF;&#x8BF4;&#x8FC7;&#x4E86;&#xFF0C;.Net&#x6846;&#x67B6;&#x4E2D;&#x7684;System.Object.GetHashCode()&#x5176;&#x5B9E;&#x5E76;&#x4E0D;&#x6EE1;&#x8DB3;&#x5747;&#x8861;&#x9AD8;&#x6548;&#x5206;&#x5E03;&#x8FD9;&#x4E00;&#x89C4;&#x5219;)&#x3002;&#x5BF9;&#x4E8E;&#x5176;&#x5B83;&#x7C7B;&#x578B;&#xFF0C;&#x5C31;&#x4EA4;&#x7ED9;&#x9ED8;&#x8BA4;&#x7684;&#x884C;&#x4E3A;&#x5427;&#xFF0C;&#x77E5;&#x9053;&#x5B83;&#x7684;&#x7F3A;&#x70B9;&#x5C31;&#x884C;&#x4E86;&#x3002;<br/>========================================================<br/>Item 10: Understand the Pitfalls of GetHashCode()<br/>This is the only item in this book dedicated to one function that you should avoid writing. GetHashCode() is used in one place only: to define the hash value for keys in a hash-based collection, typically the Hashtable o&#114; Dictionary containers. That&#39;s good because there are a number of problems with the base class implementation of GetHashCode(). For reference types, it works but is inefficient. For value types, the base class version is often incorrect. But it gets worse. It&#39;s entirely possible that you cannot write GetHashCode() so that it is both efficient and correct. No single function generates more discussion and more confusion than GetHashCode(). Read on to remove all that confusion.<br/>If you&#39;re defining a type that won&#39;t ever be used as the key in a container, this won&#39;t matter. Types that represent window controls, web page controls, o&#114; database connections are unlikely to be used as keys in a collection. In those cases, do nothing. All reference types will have a hash code that is correct, even if it is very inefficient. Value types should be immutable (see Item 7), in which case, the default implementation always works, although it is also inefficient. In most types that you cr&#101;ate, the best approach is to avoid the existence of GetHashCode() entirely.<br/>One day, you&#39;ll cr&#101;ate a type that is meant to be used as a hashtable key, and you&#39;ll need to write your own implementation of GetHashCode(), so read on. Hash-based containers use hash codes to optimize searches. Every object generates an integer value called a hash code. Objects are stored in buckets based on the value of that hash code. To search for an object, you request its key and search just that one bucket. In .NET, everyobject has a hash code, determined by System.Object.GetHashCode(). Any overload of GetHashCode() must follow these three rules:<br/>If two objects are equal (as defined by operator==), they must generate the same hash value. Otherwise, hash codes can&#39;t be used to find objects in containers.<br/>For any object A, A.GetHashCode() must be an instance invariant. No matter what methods are called on A, A.GetHashCode() must always return the same value. That ensures that an object placed in a bucket is always in the right bucket.<br/>The hash function should generate a random distribution among all integers for all inputs. That&#39;s how you get efficiency from a hash-based container.<br/>Writing a correct and efficient hash function requires extensive knowledge of the type to ensure that rule 3 is followed. The versions defined in System.Object and System.ValueType do not have that advantage. These versions must provide the best default behavior with almost no knowledge of your particular type. Object.GetHashCode() uses an internal field in the System.Object class to generate the hash value. Each object cr&#101;ated is assigned a unique object key, stored as an integer, when it is cr&#101;ated. These keys start at 1 and increment every time a new object of any type gets cr&#101;ated. The object identity field is set in the System.Object constructor and cannot be modified later. Object.GetHashCode() returns this value as the hash code for a given object.<br/>Now examine Object.GetHashCode() in light of those three rules. If two objects are equal, Object.GetHashCode()returns the same hash value, unless you&#39;ve overridden operator==. System.Object&#39;s version of operator==() tests object identity. GetHashCode() returns the internal object identity field. It works. However, if you&#39;ve supplied your own version of operator==, you must also supply your own version of GetHashCode() to ensure that the first rule is followed. See Item 9 for details on equality.<br/>The second rule is followed: After an object is cr&#101;ated, its hash code never changes.<br/>The third rule, a random distribution among all integers for all inputs, does not hold. A numeric sequence is not a random distribution among all integers unless you cr&#101;ate an enormous number of objects. The hash codes generated by Object.GetHashCode() are concentrated at the low end of the range of integers.<br/>This means that Object.GetHashCode() is correct but not efficient. If you cr&#101;ate a hashtable based on a reference type that you define, the default behavior from System.Object is a working, but slow, hashtable. When you cr&#101;ate reference types that are meant to be hash keys, you shouldoverride GetHashCode()to get a better distribution of the hash values across all integers for your specific type.<br/>Before covering how to write your own override of GetHashCode, this section examines ValueType.GetHashCode()with respect to those same three rules. System.ValueType overrides GetHashCode(), providing the default behavior for all value types. Its version returns the hash code from the first field defined in the type. Consider this example:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct MyStruct<br/>{<br/>&nbsp;&nbsp;private string&nbsp;&nbsp; _msg;<br/>&nbsp;&nbsp;private int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_id;<br/>&nbsp;&nbsp;private DateTime _epoch;<br/>}<br/>The hash code returned from a MyStruct object is the hash code generated by the _msg field. The following code snippet always returns true:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;MyStruct s = new MyStruct( );<br/>return s.GetHashCode( ) == s._msg.GetHashCode( );<br/>The first rule says that two objects that are equal (as defined by operator==()) must have the same hash code. This rule is followed for value types under most conditions, but you can break it, just as you could with for reference types. ValueType.operator==() compares the first field in the struct, along with every other field. That satisfies rule 1. As long as any override that you define for operator== uses the first field, it will work. Any struct whose first field does not participate in the equality of the type violates this rule, breaking GetHashCode().<br/>The second rule states that the hash code must be an instance invariant. That rule is followed only when the first field in the struct is an immutable field. If the value of the first field can change, so can the hash code. That breaks the rules. Yes, GetHashCode() is broken for any struct that you cr&#101;ate when the first field can be modified during the lifetime of the object. It&#39;s yet another reason why immutable value types are your best bet (see Item 7).<br/>The third rule depends on the type of the first field and how it is used. If the first field generates a random distribution across all integers, and the first field is distributed across all values of the struct, then the struct generates an even distribution as well. However, if the first field often has the same value, this rule is violated. Consider a small change to the earlier struct:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public struct MyStruct<br/>{<br/>&nbsp;&nbsp;private DateTime _epoch;<br/>&nbsp;&nbsp;private string&nbsp;&nbsp; _msg;<br/>&nbsp;&nbsp;private int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_id;<br/>}<br/>If the _epoch field is set to the current date (not including the time), all MyStruct objects cr&#101;ated in a given date will have the same hash code. That prevents an even distribution among all hash code values.<br/>Summarizing the default behavior, Object.GetHashCode() works correctly for reference types, although it does not necessarily generate an efficient distribution. (If you have overridden Object.operator==(), you can break GetHashCode()). ValueType.GetHashCode() works only if the first field in your struct is read-only. ValueType.GetHashCode() generates an efficient hash code only when the first field in your struct contains values across a meaningful subset of its inputs.<br/>If you&#39;re going to build a better hash code, you need to place some constraints on your type. Examine the three rules again, this time in the context of building a working implementation of GetHashCode().<br/>First, if two objects are equal, as defined by operator==(), they must return the same hash value. Any property o&#114; data value used to generate the hash code must also participate in the equality test for the type. Obviously, this means that the same properties used for equality are used for hash code generation. It&#39;s possible to have properties participate in equality that are not used in the hash code computation. The default behavior for System.ValueType does just that, but it often means that rule 3 usually gets violated. The same data elements should participate in both computations.<br/>The second rule is that the return value of GetHashCode() must be an instance invariant. Imagine that you defined a reference type, Customer:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class Customer<br/>{<br/>&nbsp;&nbsp;private string _name;<br/>&nbsp;&nbsp;private decimal _revenue;<br/>&nbsp;&nbsp;public Customer( string name )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_name = name;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public string Name<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;get { return _name; }<br/>&nbsp;&nbsp;&nbsp;&nbsp;set { _name = value; }<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public override int GetHashCode()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return _name.GetHashCode();<br/>&nbsp;&nbsp;}<br/>}<br/>Suppose that you execute the following code snippet:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Customer c1 = new Customer( &#34;Acme Products&#34; );<br/>myHashMap.Add( c1, o&#114;ders );<br/>// Oops, the name is wrong:<br/>c1.Name = &#34;Acme Software&#34;;<br/>c1 is lost somewh&#101;re in the hash map. When you placed c1 in the map, the hash code was generated from the string &#34;Acme Products&#34;. After you change the name of the customer to &#34;Acme Software&#34;, the hash code value changed. It&#39;s now being generated from the new name: &#34;Acme Software&#34;. C1 is stored in the bucket defined by &#34;Acme Products&#34;, but it should be in the bucket defined for &#34;Acme Software&#34;. You&#39;ve lost that customer in your own collection. It&#39;s lost because the hash code is not an object invariant. You&#39;ve changed the correct bucket after storing the object.<br/>The earlier situation can occur only if Customer is a reference type. Value types misbehave differently, but they still cause problems. If customer is a value type, a copy of c1 gets stored in the hashmap. The last line changing the value of the name has no effect on the copy stored in the hashmap. Because boxing and unboxingmake copies as well, it&#39;s very unlikely that you can change the members of a value type after that object has been added to a collection.<br/>The only way to address rule 2 is to define the hash code function to return a value based on some invariant property o&#114; properties of the object. System.Object abides by this rule using the object identity, which does not change. System.ValueType hopes that the first field in your type does not change. You can&#39;t do better without making your type immutable. When you define a value type that is intended for use as a key type in a hash container, it must be an immutable type. Violate this recommendation, and the users of your type will find a way to break hashtables that use your type as keys. Revisiting the Customer class, you can modify it so that the customer name is immutable:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class Customer<br/>{<br/>&nbsp;&nbsp;private readonly string _name;<br/>&nbsp;&nbsp;private decimal _revenue;<br/>&nbsp;&nbsp;public Customer( string name ) :<br/>&nbsp;&nbsp;&nbsp;&nbsp;this ( name, 0 )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public Customer( string name, decimal revenue )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_name = name;<br/>&nbsp;&nbsp;&nbsp;&nbsp;_revenue = revenue;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public string Name<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;get { return _name; }<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;// Change the name, returning a new object:<br/>&nbsp;&nbsp;public Customer ChangeName( string newName )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return new Customer( newName, _revenue );<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public override int GetHashCode()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return _name.GetHashCode();<br/>&nbsp;&nbsp;}<br/>}<br/>Making the name immutable changes how you must work with customer objects to modify the name:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Customer c1 = new Customer( &#34;Acme Products&#34; );<br/>myHashMap.Add( c1,orders );<br/>// Oops, the name is wrong:<br/>Customer c2 = c1.ChangeName( &#34;Acme Software&#34; );<br/>o&#114;der o = myHashMap[ c1 ] as o&#114;der;<br/>myHashMap.Remove( c1 );<br/>myHashMap.Add( c2, o );<br/>You have to remove the o&#114;iginal customer, change the name, and add the new customer object to the hashtable. It looks more cumbersome than the first version, but it works. The previous version allowed programmers to write incorrect code. By enforcing the immutability of the properties used to calculate the hash code, you enforce correct behavior. Users of your type can&#39;t go wrong. Yes, this version is more work. You&#39;re forcing developers to write more code, but only because it&#39;s the only way to write the correct code. Make certain that any data members used to calculate the hash value are immutable.<br/>The third rule says that GetHashCode() should generate a random distribution among all integers for all inputs. Satisfying this requirement depends on the specifics of the types you cr&#101;ate. If a magic formula existed, it would be implemented in System.Object and this item would not exist. A common and successful algorithm is to XOR all the return values from GetHashCode() on all fields in a type. If your type contains some mutable fields, exclude those fields from the calculations.<br/>GetHashCode() has very specific requirements: Equal objects must produce equal hash codes, and hash codes must be object invariants and must produce an even distribution to be efficient. All three can be satisfied only for immutable types. For other types, rely on the default behavior, but understand the pitfalls.</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;1.867&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;&#x31;&#x30;&#x3A;&#x20;&#x660E;&#x767D;&#x47;&#x65;&#x74;&#x48;&#x61;&#x73;&#x68;&#x43;&#x6F;&#x64;&#x65;&#x28;&#x29;&#x7684;&#x7F3A;&#x9677;</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;1.867&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;1.898&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card>
</wml>
