
<?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;&#x31;&#xFF1A;&#x9009;&#x62E9;&#x66;&#x6F;&#x72;&#x65;&#x61;&#x63;&#x68;&#x5FAA;&#x73AF;</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;&#x32;&#x30;&#x20;&#x31;&#x30;&#x3A;&#x32;&#x33;&#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> C#&#x7684;foreach&#x8BED;&#x53E5;&#x662F;&#x4ECE;do&#xFF0C;while&#xFF0C;&#x6216;&#x8005;for&#x5FAA;&#x73AF;&#x8BED;&#x53E5;&#x53D8;&#x5316;&#x800C;&#x6765;&#x7684;&#xFF0C;&#x5B83;&#x76F8;&#x5BF9;&#x8981;&#x597D;&#x4E00;&#x4E9B;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x4E3A;&#x4F60;&#x7684;&#x4EFB;&#x4F55;&#x96C6;&#x5408;&#x4EA7;&#x751F;&#x6700;&#x597D;&#x7684;&#x8FED;&#x4EE3;&#x4EE3;&#x7801;&#x3002;&#x5B83;&#x7684;&#x5B9A;&#x4E49;&#x4F9D;&#x61D2;&#x4E8E;.Net&#x6846;&#x67B6;&#x91CC;&#x7684;&#x96C6;&#x5408;&#x63A5;&#x53E3;&#xFF0C;&#x5E76;&#x4E14;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x4E3A;&#x5B9E;&#x9645;&#x7684;&#x96C6;&#x5408;&#x751F;&#x6210;&#x6700;&#x597D;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x5F53;&#x4F60;&#x5728;&#x96C6;&#x5408;&#x4E0A;&#x505A;&#x8FED;&#x4EE3;&#x65F6;&#xFF0C;&#x53EF;&#x7528;&#x4F7F;&#x7528;foreach&#x6765;&#x53D6;&#x4EE3;&#x5176;&#x5B83;&#x7684;&#x5FAA;&#x73AF;&#x7ED3;&#x6784;&#x3002;&#x68C0;&#x67E5;&#x4E0B;&#x9762;&#x7684;&#x4E09;&#x4E2A;&#x5FAA;&#x73AF;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;int [] foo = new int[100];<br/>// Loop 1:<br/>foreach ( int i in foo)<br/>&nbsp;&nbsp;Console.WriteLine( i.ToString( ));<br/>// Loop 2:<br/>for ( int index = 0;&nbsp;&nbsp;index &lt; foo.Length;&nbsp;&nbsp;index++ )<br/>&nbsp;&nbsp;Console.WriteLine( foo[index].ToString( ));<br/>// Loop 3:<br/>int len = foo.Length;<br/>for ( int index = 0;&nbsp;&nbsp;index &lt; len;&nbsp;&nbsp;index++ )<br/>&nbsp;&nbsp;Console.WriteLine( foo[index].ToString( ));<br/>&#x5BF9;&#x4E8E;&#x5F53;&#x524D;&#x7684;C#&#x7F16;&#x8BD1;&#x5668;(&#x7248;&#x672C;1.1&#x6216;&#x8005;&#x66F4;&#x9AD8;)&#x800C;&#x8A00;&#xFF0C;&#x5FAA;&#x73AF;1&#x662F;&#x6700;&#x597D;&#x7684;&#x3002;&#x8D77;&#x7801;&#x5B83;&#x7684;&#x8F93;&#x5165;&#x8981;&#x5C11;&#x4E9B;&#xFF0C;&#x8FD9;&#x4F1A;&#x4F7F;&#x4F60;&#x7684;&#x4E2A;&#x4EBA;&#x5F00;&#x53D1;&#x6548;&#x7387;&#x63D0;&#x63D0;&#x5347;&#x3002;(1.0&#x7684;C#&#x7F16;&#x8BD1;&#x5668;&#x5BF9;&#x5FAA;&#x73AF;1&#x800C;&#x8A00;&#x8981;&#x6162;&#x5F88;&#x591A;&#xFF0C;&#x6240;&#x4EE5;&#x5BF9;&#x4E8E;&#x90A3;&#x4E2A;&#x7248;&#x672C;&#x5FAA;&#x73AF;2&#x662F;&#x6700;&#x597D;&#x7684;&#x3002;) &#x5FAA;&#x73AF;3&#xFF0C;&#x5927;&#x591A;&#x6570;C&#x6216;&#x8005;C++&#x7A0B;&#x5E8F;&#x5458;&#x4F1A;&#x8BA4;&#x4E3A;&#x5B83;&#x662F;&#x6700;&#x6709;&#x6548;&#x7684;&#xFF0C;&#x4F46;&#x5B83;&#x662F;&#x6700;&#x7CDF;&#x7CD5;&#x7684;&#x3002;&#x56E0;&#x4E3A;&#x5728;&#x5FAA;&#x73AF;&#x5916;&#x90E8;&#x53D6;&#x51FA;&#x4E86;&#x53D8;&#x91CF;Length&#x7684;&#x503C;&#xFF0C;&#x4ECE;&#x800C;&#x963B;&#x788D;&#x4E86;JIT&#x7F16;&#x8BD1;&#x5668;&#x5C06;&#x8FB9;&#x754C;&#x68C0;&#x6D4B;&#x4ECE;&#x5FAA;&#x73AF;&#x4E2D;&#x79FB;&#x51FA;&#x3002;<br/>C#&#x4EE3;&#x7801;&#x662F;&#x5B89;&#x5168;&#x7684;&#x6258;&#x7BA1;&#x4EE3;&#x7801;&#x91CC;&#x8FD0;&#x884C;&#x7684;&#x3002;&#x73AF;&#x5883;&#x91CC;&#x7684;&#x6BCF;&#x4E00;&#x5757;&#x5185;&#x5B58;&#xFF0C;&#x5305;&#x62EC;&#x6570;&#x636E;&#x7684;&#x7D22;&#x5F15;&#xFF0C;&#x90FD;&#x662F;&#x88AB;&#x76D1;&#x89C6;&#x7684;&#x3002;&#x7A0D;&#x5FAE;&#x5C55;&#x5F00;&#x4E00;&#x4E0B;&#xFF0C;&#x5FAA;&#x73AF;3&#x7684;&#x4EE3;&#x7801;&#x5B9E;&#x9645;&#x5F88;&#x50CF;&#x8FD9;&#x6837;&#x7684;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;// Loop 3, as generated by compiler:<br/>int len = foo.Length;<br/>for ( int index = 0;&nbsp;&nbsp;index &lt; len;&nbsp;&nbsp;index++ )<br/>{<br/>&nbsp;&nbsp;if ( index &lt; foo.Length )<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( foo[index].ToString( ));<br/>&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;&nbsp;throw new IndexOutOfRangeException( );<br/>}<br/>C#&#x7684;JIT&#x7F16;&#x8BD1;&#x5668;&#x8DDF;&#x4F60;&#x4E0D;&#x4E00;&#x6837;&#xFF0C;&#x5B83;&#x8BD5;&#x56FE;&#x5E2E;&#x4F60;&#x8FD9;&#x6837;&#x505A;&#x4E86;&#x3002;&#x4F60;&#x672C;&#x60F3;&#x628A;Length&#x5C5E;&#x6027;&#x63D0;&#x51FA;&#x5230;&#x5FAA;&#x73AF;&#x5916;&#x9762;&#xFF0C;&#x5374;&#x4F7F;&#x5F97;&#x7F16;&#x8BD1;&#x505A;&#x4E86;&#x66F4;&#x591A;&#x7684;&#x4E8B;&#x60C5;&#xFF0C;&#x4ECE;&#x800C;&#x4E5F;&#x964D;&#x4F4E;&#x4E86;&#x901F;&#x5EA6;&#x3002;CLR&#x8981;&#x4FDD;&#x8BC1;&#x7684;&#x5185;&#x5BB9;&#x4E4B;&#x4E00;&#x5C31;&#x662F;&#xFF1A;&#x4F60;&#x4E0D;&#x80FD;&#x5199;&#x51FA;&#x8BA9;&#x53D8;&#x91CF;&#x8BBF;&#x95EE;&#x4E0D;&#x5C5E;&#x4E8E;&#x5B83;&#x81EA;&#x5DF1;&#x5185;&#x5B58;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x5728;&#x8BBF;&#x95EE;&#x6BCF;&#x4E00;&#x4E2A;&#x5B9E;&#x9645;&#x7684;&#x96C6;&#x5408;&#x65F6;&#xFF0C;&#x8FD0;&#x884C;&#x65F6;&#x786E;&#x4FDD;&#x5BF9;&#x6BCF;&#x4E2A;&#x96C6;&#x5408;&#x7684;&#x8FB9;&#x754C;(&#x4E0D;&#x662F;len&#x53D8;&#x91CF;)&#x505A;&#x4E86;&#x68C0;&#x6D4B;&#x3002;&#x4F60;&#x628A;&#x4E00;&#x4E2A;&#x8FB9;&#x754C;&#x68C0;&#x6D4B;&#x5206;&#x6210;&#x4E86;&#x4E24;&#x4E2A;&#x3002;<br/>&#x4F60;&#x8FD8;&#x662F;&#x8981;&#x4E3A;&#x5FAA;&#x73AF;&#x7684;&#x6BCF;&#x4E00;&#x6B21;&#x8FED;&#x4EE3;&#x505A;&#x6570;&#x7EC4;&#x505A;&#x7D22;&#x5F15;&#x68C0;&#x6D4B;&#xFF0C;&#x800C;&#x4E14;&#x662F;&#x4E24;&#x6B21;&#x3002;&#x5FAA;&#x73AF;1&#x548C;&#x5FAA;&#x73AF;2&#x8981;&#x5FEB;&#x4E00;&#x4E9B;&#x7684;&#x539F;&#x56E0;&#x662F;&#x56E0;&#x4E3A;&#xFF0C;C#&#x7684;JIT&#x7F16;&#x8BD1;&#x5668;&#x53EF;&#x4EE5;&#x9A8C;&#x8BC1;&#x6570;&#x7EC4;&#x7684;&#x8FB9;&#x754C;&#x6765;&#x786E;&#x4FDD;&#x5B89;&#x5168;&#x3002;&#x4EFB;&#x4F55;&#x5FAA;&#x73AF;&#x53D8;&#x91CF;&#x4E0D;&#x662F;&#x6570;&#x636E;&#x7684;&#x957F;&#x5EA6;&#x65F6;&#xFF0C;&#x8FB9;&#x754C;&#x68C0;&#x6D4B;&#x5C31;&#x4F1A;&#x5728;&#x6BCF;&#x4E00;&#x6B21;&#x8FED;&#x4EE3;&#x4E2D;&#x53D1;&#x751F;&#x3002;(&#x8BD1;&#x6CE8;&#xFF1A;&#x8FD9;&#x91CC;&#x51E0;&#x6B21;&#x8BF4;&#x5230;JIT&#x7F16;&#x8BD1;&#x5668;&#xFF0C;&#x5B83;&#x662F;&#x6307;&#x5C06;IL&#x4EE3;&#x7801;&#x7F16;&#x8BD1;&#x6210;&#x672C;&#x5730;&#x4EE3;&#x7801;&#x65F6;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x6307;&#x5C06;C#&#x4EE3;&#x7801;&#x6216;&#x8005;&#x5176;&#x5B83;&#x4EE3;&#x7801;&#x7F16;&#x8BD1;&#x6210;IL&#x4EE3;&#x7801;&#x65F6;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x3002;&#x5176;&#x5B9E;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;&#x4E0D;&#x5B89;&#x5168;&#x9009;&#x9879;&#x6765;&#x8FEB;&#x4F7F;JIT&#x4E0D;&#x505A;&#x8FD9;&#x6837;&#x7684;&#x68C0;&#x6D4B;&#xFF0C;&#x4ECE;&#x800C;&#x4F7F;&#x8FD0;&#x884C;&#x901F;&#x5EA6;&#x63D0;&#x9AD8;&#x3002;)<br/>&#x539F;&#x59CB;&#x7684;C#&#x7F16;&#x8BD1;&#x5668;&#x4E4B;&#x6240;&#x4EE5;&#x5BF9;foreach&#x4EE5;&#x53CA;&#x6570;&#x7EC4;&#x4EA7;&#x751F;&#x5F88;&#x6162;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x6D89;&#x53CA;&#x5230;&#x4E86;&#x88C5;&#x7BB1;&#x3002;&#x88C5;&#x7BB1;&#x4F1A;&#x5728;&#x539F;&#x5219;17&#x4E2D;&#x5C55;&#x5F00;&#x8BA8;&#x8BBA;&#x3002;&#x6570;&#x7EC4;&#x662F;&#x5B89;&#x5168;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x73B0;&#x5728;&#x7684;foreach&#x53EF;&#x4EE5;&#x4E3A;&#x6570;&#x7EC4;&#x751F;&#x6210;&#x4E0E;&#x5176;&#x5B83;&#x96C6;&#x5408;&#x4E0D;&#x540C;&#x7684;IL&#x4EE3;&#x7801;&#x3002;&#x5BF9;&#x4E8E;&#x6570;&#x7EC4;&#x7684;&#x8FD9;&#x4E2A;&#x7248;&#x672C;&#xFF0C;&#x5B83;&#x4E0D;&#x518D;&#x4F7F;&#x7528;IEnumerator&#x63A5;&#x53E3;&#xFF0C;&#x5C31;&#x662F;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x987B;&#x8981;&#x88C5;&#x7BB1;&#x4E0E;&#x62C6;&#x7BB1;&#x3002;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;IEnumerator it = foo.GetEnumerator( );<br/>while( it.MoveNext( ))<br/>{<br/>&nbsp;&nbsp;int i = (int) it.Current; // box and unbox here.<br/>&nbsp;&nbsp;Console.WriteLine( i.ToString( ) );<br/>}<br/>&#x53D6;&#x800C;&#x4EE3;&#x4E4B;&#x7684;&#x662F;&#xFF0C;foreach&#x8BED;&#x53E5;&#x4E3A;&#x6570;&#x7EC4;&#x751F;&#x6210;&#x4E86;&#x8FD9;&#x6837;&#x7684;&#x7ED3;&#x6784;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;for ( int index = 0;&nbsp;&nbsp;index &lt; foo.Length;&nbsp;&nbsp;index++ )<br/>&nbsp;&nbsp;Console.WriteLine( foo[index].ToString( ));<br/>(&#x8BD1;&#x6CE8;&#xFF1A;&#x6CE8;&#x610F;&#x6570;&#x7EC4;&#x4E0E;&#x96C6;&#x5408;&#x7684;&#x533A;&#x522B;&#x3002;&#x6570;&#x7EC4;&#x662F;&#x4E00;&#x6B21;&#x6027;&#x5206;&#x914D;&#x7684;&#x8FDE;&#x7EED;&#x5185;&#x5B58;&#xFF0C;&#x96C6;&#x5408;&#x662F;&#x53EF;&#x4EE5;&#x52A8;&#x6001;&#x6DFB;&#x52A0;&#x4E0E;&#x4FEE;&#x6539;&#x7684;&#xFF0C;&#x4E00;&#x822C;&#x7528;&#x94FE;&#x8868;&#x6765;&#x5B9E;&#x73B0;&#x3002;&#x800C;&#x5BF9;&#x4E8E;C#&#x91CC;&#x6240;&#x652F;&#x6301;&#x7684;&#x952F;&#x9F7F;&#x6570;&#x7EC4;&#xFF0C;&#x5219;&#x662F;&#x4E00;&#x79CD;&#x6298;&#x8877;&#x7684;&#x5904;&#x7406;&#x3002;)<br/>foreach&#x603B;&#x80FD;&#x4FDD;&#x8BC1;&#x6700;&#x597D;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x4F60;&#x4E0D;&#x7528;&#x64CD;&#x5FC3;&#x54EA;&#x79CD;&#x7ED3;&#x6784;&#x7684;&#x5FAA;&#x73AF;&#x6709;&#x66F4;&#x9AD8;&#x7684;&#x6548;&#x7387;&#xFF1A;foreach&#x548C;&#x7F16;&#x8BD1;&#x5668;&#x4E3A;&#x4F60;&#x4EE3;&#x52B3;&#x4E86;&#x3002;<br/>&#x5982;&#x679C;&#x4F60;&#x5E76;&#x4E0D;&#x6EE1;&#x8DB3;&#x4E8E;&#x9AD8;&#x6548;&#xFF0C;&#x4F8B;&#x5982;&#x8FD8;&#x8981;&#x6709;&#x8BED;&#x8A00;&#x7684;&#x4EA4;&#x4E92;&#x3002;&#x8FD9;&#x4E2A;&#x4E16;&#x754C;&#x4E0A;&#x6709;&#x4E9B;&#x4EBA;(&#x662F;&#x7684;&#xFF0C;&#x6B63;&#x662F;&#x4ED6;&#x4EEC;&#x5728;&#x4F7F;&#x7528;&#x5176;&#x5B83;&#x7684;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;)&#x575A;&#x5B9A;&#x4E0D;&#x79FB;&#x7684;&#x8BA4;&#x4E3A;&#x6570;&#x7EC4;&#x7684;&#x7D22;&#x5F15;&#x662F;&#x4ECE;1&#x5F00;&#x59CB;&#x7684;&#xFF0C;&#x800C;&#x4E0D;&#x662F;0&#x3002;&#x4E0D;&#x7BA1;&#x6211;&#x4EEC;&#x5982;&#x4F55;&#x52AA;&#x529B;&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x65E0;&#x6CD5;&#x7834;&#x9664;&#x4ED6;&#x4EEC;&#x7684;&#x8FD9;&#x79CD;&#x4E60;&#x60EF;&#x3002;.Net&#x5F00;&#x53D1;&#x7EC4;&#x5DF2;&#x7ECF;&#x5C1D;&#x8BD5;&#x8FC7;&#x3002;&#x4E3A;&#x6B64;&#x4F60;&#x4E0D;&#x5F97;&#x4E0D;&#x5728;C#&#x8FD9;&#x6837;&#x5199;&#x521D;&#x59CB;&#x5316;&#x4EE3;&#x7801;&#xFF0C;&#x90A3;&#x5C31;&#x662F;&#x6570;&#x7EC4;&#x4ECE;&#x67D0;&#x4E2A;&#x975E;0&#x6570;&#x503C;&#x5F00;&#x59CB;&#x7684;&#x3002;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;// Cr&#101;ate a single dimension array.<br/>// Its range is [ 1 .. 5 ]<br/>Array test = Array.Cr&#101;ateInstance( typeof( int ),<br/>new int[ ]{ 5 }, new int[ ]{ 1 });<br/>&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x5E94;&#x8BE5;&#x8DB3;&#x591F;&#x8BA9;&#x6240;&#x6709;&#x4EBA;&#x611F;&#x5230;&#x754F;&#x60E7;&#x4E86;(&#x8BD1;&#x6CE8;&#xFF1A;&#x5BF9;&#x6211;&#x800C;&#x8A00;&#xFF0C;&#x786E;&#x5B9E;&#x6709;&#x4E00;&#x70B9;)&#x3002;&#x4F46;&#x6709;&#x4E9B;&#x4EBA;&#x5C31;&#x662F;&#x5F88;&#x987D;&#x56FA;&#xFF0C;&#x65E0;&#x8BA4;&#x4F60;&#x5982;&#x4F55;&#x52AA;&#x529B;&#xFF0C;&#x4ED6;&#x4EEC;&#x4F1A;&#x4ECE;1&#x5F00;&#x59CB;&#x8BA1;&#x6570;&#x3002;&#x5F88;&#x5E78;&#x8FD0;&#xFF0C;&#x8FD9;&#x662F;&#x90A3;&#x4E9B;&#x95EE;&#x9898;&#x5F53;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#xFF0C;&#x800C;&#x4F60;&#x53EF;&#x4EE5;&#x8BA9;&#x7F16;&#x8BD1;&#x5668;&#x6765;&#x201C;&#x6B3A;&#x9A97;&#x201D;&#x3002;&#x7528;foreach&#x6765;&#x5BF9;test&#x6570;&#x7EC4;&#x8FDB;&#x884C;&#x8FED;&#x4EE3;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;foreach( int j in test )<br/>&nbsp;&nbsp;Console.WriteLine ( j );<br/>foreach&#x8BED;&#x53E5;&#x77E5;&#x9053;&#x5982;&#x4F55;&#x68C0;&#x6D4B;&#x6570;&#x7EC4;&#x7684;&#x4E0A;&#x4E0B;&#x9650;&#xFF0C;&#x6240;&#x4EE5;&#x4F60;&#x5E94;&#x8BE5;&#x8FD9;&#x6837;&#x505A;&#xFF0C;&#x800C;&#x4E14;&#x8FD9;&#x548C;for&#x5FAA;&#x73AF;&#x7684;&#x901F;&#x5EA6;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x4E5F;&#x4E0D;&#x7528;&#x7BA1;&#x67D0;&#x4EBA;&#x662F;&#x91C7;&#x7528;&#x90A3;&#x4E2A;&#x505A;&#x4E3A;&#x4E0B;&#x754C;&#x3002;<br/>&#x5BF9;&#x4E8E;&#x591A;&#x7EF4;&#x6570;&#x7EC4;&#xFF0C;foreach&#x7ED9;&#x4E86;&#x4F60;&#x540C;&#x6837;&#x7684;&#x597D;&#x5904;&#x3002;&#x5047;&#x8BBE;&#x4F60;&#x6B63;&#x5728;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x68CB;&#x76D8;&#x3002;&#x4F60;&#x5C06;&#x4F1A;&#x8FD9;&#x6837;&#x5199;&#x4E24;&#x6BB5;&#x4EE3;&#x7801;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;private Square[,] _theBoard = new Square[ 8, 8 ];<br/>// elsewh&#101;re in code:<br/>for ( int i = 0; i &lt; _theBoard.GetLength( 0 ); i++ )<br/>&nbsp;&nbsp;for( int j = 0; j &lt; _theBoard.GetLength( 1 ); j++ )<br/>&nbsp;&nbsp;&nbsp;&nbsp;_theBoard[ i, j ].PaintSquare( );<br/>&#x53D6;&#x800C;&#x4EE3;&#x4E4B;&#x7684;&#x662F;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x7B80;&#x5355;&#x7684;&#x753B;&#x8FD9;&#x4E2A;&#x68CB;&#x76D8;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;foreach( Square sq in _theBoard )<br/>&nbsp;&nbsp;sq.PaintSquare( );<br/>(&#x8BD1;&#x6CE8;&#xFF1A;&#x672C;&#x4EBA;&#x4E0D;&#x8D5E;&#x6210;&#x8FD9;&#x6837;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x5B83;&#x9690;&#x85CF;&#x4E86;&#x6570;&#x7EC4;&#x7684;&#x884C;&#x4E0E;&#x5217;&#x7684;&#x903B;&#x8F91;&#x5173;&#x7CFB;&#x3002;&#x5FAA;&#x73AF;&#x662F;&#x4EE5;&#x884C;&#x4F18;&#x5148;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x8981;&#x7684;&#x4E0D;&#x662F;&#x8FD9;&#x4E2A;&#x987A;&#x5E8F;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x79CD;&#x5FAA;&#x73AF;&#x5E76;&#x4E0D;&#x597D;&#x3002;)<br/>foreach&#x8BED;&#x53E5;&#x751F;&#x6210;&#x6070;&#x5F53;&#x7684;&#x4EE3;&#x7801;&#x6765;&#x8FED;&#x4EE3;&#x6570;&#x7EC4;&#x91CC;&#x6240;&#x6709;&#x7EF4;&#x6570;&#x7684;&#x6570;&#x636E;&#x3002;&#x5982;&#x679C;&#x5C06;&#x6765;&#x4F60;&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;3D&#x7684;&#x68CB;&#x76D8;&#xFF0C;foreach&#x5FAA;&#x73AF;&#x8FD8;&#x662F;&#x4E00;&#x6837;&#x7684;&#x5DE5;&#x4F5C;&#xFF0C;&#x800C;&#x53E6;&#x4E00;&#x4E2A;&#x5FAA;&#x73AF;&#x5219;&#x8981;&#x505A;&#x8FD9;&#x6837;&#x7684;&#x4FEE;&#x6539;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;for ( int i = 0; i &lt; _theBoard.GetLength( 0 ); i++ )<br/>&nbsp;&nbsp;for( int j = 0; j &lt; _theBoard.GetLength( 1 ); j++ )<br/>&nbsp;&nbsp;&nbsp;&nbsp;for( int k = 0; k &lt; _theBoard.GetLength( 2 ); k++ )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_theBoard[ i, j, k ].PaintSquare( );<br/>(&#x8BD1;&#x6CE8;&#xFF1A;&#x8FD9;&#x6837;&#x770B;&#x4E0A;&#x53BB;&#x867D;&#x7136;&#x4EE3;&#x7801;&#x5F88;&#x591A;&#xFF0C;&#x4F46;&#x6211;&#x89C9;&#x5F97;&#xFF0C;&#x53EA;&#x8981;&#x662F;&#x7A0B;&#x5E8F;&#x5458;&#x90FD;&#x53EF;&#x4EE5;&#x4E00;&#x773C;&#x770B;&#x51FA;&#x8FD9;&#x662F;&#x4E2A;&#x4E09;&#x7EF4;&#x6570;&#x7EC4;&#x7684;&#x5FAA;&#x73AF;&#xFF0C;&#x4F46;&#x662F;&#x5BF9;&#x4E8E;foreach&#xFF0C;&#x6211;&#x770B;&#x6CA1;&#x4EBA;&#x4E00;&#x773C;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#x6765;&#x5B83;&#x5728;&#x505A;&#x4EC0;&#x4E48;! &#x4E2A;&#x4EBA;&#x7406;&#x89E3;&#x3002;&#x5F53;&#x7136;&#xFF0C;&#x8FD9;&#x8981;&#x770B;&#x4F60;&#x600E;&#x6837;&#x8BA4;&#x8BC6;&#xFF0C;&#x8FD9;&#x5F53;&#x7136;&#x53EF;&#x4EE5;&#x8BF4;&#x662F;foreach&#x7684;&#x4E00;&#x4E2A;&#x4F18;&#x70B9;&#x3002;)<br/>&#x4E8B;&#x5B9E;&#x4E0A;&#xFF0C;foreach&#x5FAA;&#x73AF;&#x8FD8;&#x53EF;&#x4EE5;&#x5728;&#x6BCF;&#x4E2A;&#x7EF4;&#x7684;&#x4E0B;&#x9650;&#x4E0D;&#x540C;&#x7684;&#x591A;&#x7EF4;&#x6570;&#x7EC4;&#x4E0A;&#x5DE5;&#x4F5C;(&#x8BD1;&#x6CE8;&#xFF1A;&#x4E5F;&#x5C31;&#x662F;&#x952F;&#x9F7F;&#x6570;&#x7EC4;)&#x3002; &#x6211;&#x4E0D;&#x60F3;&#x5199;&#x8FD9;&#x6837;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5373;&#x4F7F;&#x662F;&#x4E3A;&#x4E86;&#x505A;&#x4F8B;&#x793A;&#x3002;&#x4F46;&#x5F53;&#x67D0;&#x4EBA;&#x5728;&#x67D0;&#x65F6;&#x5199;&#x4E86;&#x8FD9;&#x6837;&#x7684;&#x96C6;&#x5408;&#x65F6;&#xFF0C;foreach&#x53EF;&#x4EE5;&#x80DC;&#x4EFB;&#x3002;<br/>foreach&#x4E5F;&#x7ED9;&#x4E86;&#x4F60;&#x5F88;&#x5927;&#x7684;&#x4F38;&#x7F29;&#x6027;&#xFF0C;&#x5F53;&#x67D0;&#x65F6;&#x4F60;&#x53D1;&#x73B0;&#x987B;&#x8981;&#x4FEE;&#x6539;&#x6570;&#x7EC4;&#x91CC;&#x5E95;&#x5C42;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x65F6;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x5C3D;&#x53EF;&#x80FD;&#x591A;&#x7684;&#x4FDD;&#x8BC1;&#x4EE3;&#x7801;&#x4E0D;&#x505A;&#x4FEE;&#x6539;&#x3002;&#x6211;&#x4EEC;&#x4ECE;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x6570;&#x7EC4;&#x6765;&#x8BA8;&#x8BBA;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;int [] foo = new int[100];<br/>&#x5047;&#x8BBE;&#x540E;&#x6765;&#x67D0;&#x4E9B;&#x65F6;&#x5019;&#xFF0C;&#x4F60;&#x53D1;&#x73B0;&#x5B83;&#x4E0D;&#x5177;&#x5907;&#x6570;&#x7EC4;&#x7C7B;(array class)&#x7684;&#x4E00;&#x4E9B;&#x529F;&#x80FD;&#xFF0C;&#x800C;&#x4F60;&#x53C8;&#x6B63;&#x597D;&#x8981;&#x8FD9;&#x4E9B;&#x529F;&#x80FD;&#x3002;&#x4F60;&#x53EF;&#x80FD;&#x7B80;&#x5355;&#x628A;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x4FEE;&#x6539;&#x4E3A;ArrayList:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;// Set the initial size:<br/>ArrayList foo = new ArrayList( 100 );<br/>&#x4EFB;&#x4F55;&#x7528;for&#x5FAA;&#x73AF;&#x7684;&#x4EE3;&#x7801;&#x88AB;&#x7834;&#x574F;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;int sum = 0;<br/>for ( int index = 0;<br/>&nbsp;&nbsp;// won&#39;t compile: ArrayList uses Count, not Length<br/>&nbsp;&nbsp;index &lt; foo.Length;<br/>&nbsp;&nbsp;index++ )<br/>&nbsp;&nbsp;// won&#39;t compile: foo[ index ] is object, not int.<br/>&nbsp;&nbsp;sum += foo[ index ];<br/>&#x7136;&#x800C;&#xFF0C;foreach&#x5FAA;&#x73AF;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x6240;&#x64CD;&#x4F5C;&#x7684;&#x5BF9;&#x8C61;&#x4E0D;&#x540C;&#xFF0C;&#x800C;&#x81EA;&#x52A8;&#x7F16;&#x8BD1;&#x6210;&#x4E0D;&#x540C;&#x7684;&#x4EE3;&#x7801;&#x6765;&#x8F6C;&#x5316;&#x6070;&#x5F53;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x4EC0;&#x4E48;&#x4E5F;&#x4E0D;&#x7528;&#x6539;&#x3002;&#x8FD8;&#x4E0D;&#x53EA;&#x662F;&#x5BF9;&#x6807;&#x51C6;&#x7684;&#x6570;&#x7EC4;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#xFF0C;&#x5BF9;&#x4E8E;&#x5176;&#x5B83;&#x4EFB;&#x4F55;&#x7684;&#x96C6;&#x5408;&#x7C7B;&#x578B;&#x4E5F;&#x540C;&#x6837;&#x53EF;&#x4EE5;&#x7528;foreach.<br/>&#x5982;&#x679C;&#x4F60;&#x7684;&#x96C6;&#x5408;&#x652F;&#x6301;.Net&#x73AF;&#x5883;&#x4E0B;&#x7684;&#x89C4;&#x5219;&#xFF0C;&#x4F60;&#x7684;&#x7528;&#x6237;&#x5C31;&#x53EF;&#x4EE5;&#x7528;foreach&#x6765;&#x8FED;&#x4EE3;&#x4F60;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;&#x4E3A;&#x4E86;&#x8BA9;foreach&#x8BED;&#x53E5;&#x8BA4;&#x4E3A;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x96C6;&#x5408;&#x7C7B;&#x578B;&#xFF0C;&#x4E00;&#x4E2A;&#x7C7B;&#x5E94;&#x8BE5;&#x6709;&#x591A;&#x6570;&#x5C5E;&#x6027;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#xFF1A;&#x516C;&#x5F00;&#x65B9;&#x6CD5;GetEnumerator()&#x7684;&#x5B9E;&#x73B0;&#x53EF;&#x4EE5;&#x6784;&#x6210;&#x4E00;&#x4E2A;&#x96C6;&#x5408;&#x7C7B;&#x3002;&#x660E;&#x786E;&#x7684;&#x5B9E;&#x73B0;IEnumerable&#x63A5;&#x53E3;&#x53EF;&#x4EE5;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x96C6;&#x5408;&#x7C7B;&#x3002;&#x5B9E;&#x73B0;IEnumerator&#x63A5;&#x53E3;&#x4E5F;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x96C6;&#x5408;&#x7C7B;&#x3002;foreach&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x4E0A;&#x5DE5;&#x4F5C;&#x3002;<br/>foreach&#x6709;&#x4E00;&#x4E2A;&#x597D;&#x5904;&#x5C31;&#x662F;&#x5173;&#x4E8E;&#x8D44;&#x6E90;&#x7BA1;&#x7406;&#x3002;IEnumerable&#x63A5;&#x53E3;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF1A;GetEnumerator()&#x3002;foreach&#x8BED;&#x53E5;&#x662F;&#x4E00;&#x4E2A;&#x5728;&#x53EF;&#x679A;&#x4E3E;&#x7684;&#x7C7B;&#x578B;&#x4E0A;&#x751F;&#x6210;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x4F18;&#x5316;&#x8FC7;&#x7684;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;IEnumerator it = foo.GetEnumerator( ) as IEnumerator;<br/>using ( IDisposable disp = it as IDisposable )<br/>{<br/>&nbsp;&nbsp;while ( it.MoveNext( ))<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;int elem = ( int ) it.Current;<br/>&nbsp;&nbsp;&nbsp;&nbsp;sum += elem;<br/>&nbsp;&nbsp;}<br/>}<br/>&#x5982;&#x679C;&#x65AD;&#x5B9A;&#x679A;&#x4E3E;&#x5668;&#x5B9E;&#x73B0;&#x4E86;IDisposable&#x63A5;&#x53E3;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x4F18;&#x5316;&#x4EE3;&#x7801;&#x4E3A;finally&#x5757;&#x3002;&#x4F46;&#x5BF9;&#x4F60;&#x800C;&#x8A00;&#xFF0C;&#x660E;&#x767D;&#x8FD9;&#x4E00;&#x70B9;&#x5F88;&#x91CD;&#x8981;&#xFF0C;&#x65E0;&#x8BBA;&#x5982;&#x4F55;&#xFF0C;foreach&#x751F;&#x6210;&#x4E86;&#x6B63;&#x786E;&#x7684;&#x4EE3;&#x7801;&#x3002;<br/>foreach&#x662F;&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#x5E7F;&#x6CDB;&#x7684;&#x8BED;&#x53E5;&#x3002;&#x5B83;&#x4E3A;&#x6570;&#x7EC4;&#x7684;&#x4E0A;&#x4E0B;&#x9650;&#x81EA;&#x6210;&#x6B63;&#x786E;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x8FED;&#x4EE3;&#x591A;&#x7EF4;&#x6570;&#x7EC4;&#xFF0C;&#x5F3A;&#x5236;&#x8F6C;&#x5316;&#x4E3A;&#x6070;&#x5F53;&#x7684;&#x7C7B;&#x578B;(&#x4F7F;&#x7528;&#x6700;&#x6709;&#x6548;&#x7684;&#x7ED3;&#x6784;)&#xFF0C;&#x8FD8;&#x6709;&#xFF0C;&#x8FD9;&#x662F;&#x6700;&#x91CD;&#x8981;&#x7684;&#xFF0C;&#x751F;&#x6210;&#x6700;&#x6709;&#x6548;&#x7684;&#x5FAA;&#x73AF;&#x7ED3;&#x6784;&#x3002;&#x8FD9;&#x662F;&#x8FED;&#x4EE3;&#x96C6;&#x5408;&#x6700;&#x6709;&#x6548;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x4F60;&#x5199;&#x51FA;&#x7684;&#x4EE3;&#x7801;&#x66F4;&#x6301;&#x4E45;(&#x8BD1;&#x6CE8;&#xFF1A;&#x5C31;&#x662F;&#x4E0D;&#x4F1A;&#x56E0;&#x4E3A;&#x9519;&#x8BEF;&#x800C;&#x6539;&#x52A8;&#x592A;&#x591A;&#x7684;&#x4EE3;&#x7801;)&#xFF0C;&#x7B2C;&#x4E00;&#x6B21;&#x5199;&#x4EE3;&#x7801;&#x7684;&#x65F6;&#x5019;&#x66F4;&#x7B80;&#x6D01;&#x3002;&#x8FD9;&#x5BF9;&#x751F;&#x4EA7;&#x529B;&#x662F;&#x4E00;&#x4E2A;&#x5C0F;&#x7684;&#x8FDB;&#x6B65;&#xFF0C;&#x968F;&#x7740;&#x65F6;&#x95F4;&#x7684;&#x63A8;&#x79FB;&#x4F1A;&#x7D2F;&#x52A0;&#x8D77;&#x6765;&#x3002;<br/>========================================================<br/>Item 11: Prefer foreach Loops<br/>The C# foreach statement is more than just a variation of the do, while, o&#114; for loops. It generates the best iteration code for any collection you have. Its definition is tied to the collection interfaces in the .NET Framework, and the C# compiler generates the best code for the particular type of collection. When you iterate collections, use foreach instead of other looping constructs. Examine these three loops:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;int [] foo = new int[100];<br/>// Loop 1:<br/>foreach ( int i in foo)<br/>&nbsp;&nbsp;Console.WriteLine( i.ToString( ));<br/>// Loop 2:<br/>for ( int index = 0;<br/>&nbsp;&nbsp;index &lt; foo.Length;<br/>&nbsp;&nbsp;index++ )<br/>&nbsp;&nbsp;Console.WriteLine( foo[index].ToString( ));<br/>// Loop 3:<br/>int len = foo.Length;<br/>for ( int index = 0;<br/>&nbsp;&nbsp;index &lt; len;<br/>&nbsp;&nbsp;index++ )<br/>&nbsp;&nbsp;Console.WriteLine( foo[index].ToString( ));<br/>For the current and future C# compilers (version 1.1 and up), loop 1 is best. It&#39;s even less typing, so your personal productivity goes up. (The C# 1.0 compiler produced much slower code for loop 1, so loop 2 is best in that version.) Loop 3, the construct most C and C++ programmers would view as most efficient, is the worst option. By hoisting the Length variable out of the loop, you make a change that hinders the JIT compiler&#39;s chance to remove range checking inside the loop.<br/>C# code runs in a safe, managed environment. Every memory location is checked, including array indexes. Taking a few liberties, the actual code for loop 3 is something like this:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;// Loop 3, as generated by compiler:<br/>int len = foo.Length;<br/>for ( int index = 0;<br/>&nbsp;&nbsp;index &lt; len;<br/>&nbsp;&nbsp;index++ )<br/>{<br/>&nbsp;&nbsp;if ( index &lt; foo.Length )<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( foo[index].ToString( ));<br/>&nbsp;&nbsp;else<br/>&nbsp;&nbsp;&nbsp;&nbsp;throw new IndexOutOfRangeException( );<br/>}<br/>The JIT C# compiler just doesn&#39;t like you trying to help it this way. Your attempt to hoist the Length property access out of the loop just made the JIT compiler do more work to generate even slower code. One of the CLR guarantees is that you cannot write code that overruns the memory that your variables own. The runtime generates a test of the actual array bounds (not your len variable) before accessing each particular array element. You get one bounds check for the price of two.<br/>You still pay to check the array index on every iteration of the loop, and you do so twice. The reason loops 1 and 2 are faster is that the C# compiler and the JIT compiler can verify that the bounds of the loop are guaranteed to be safe. Anytime the loop variable is not the length of the array, the bounds check is performed on each iteration.<br/>The reason that foreach and arrays generated very slow code in the o&#114;iginal C# compiler concerns boxing, which is covered extensively in Item 17. Arrays are type safe. foreach now generates different IL for arrays than other collections. The array version does not use the IEnumerator interface, which would require boxing and unboxing:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;IEnumerator it = foo.GetEnumerator( );<br/>while( it.MoveNext( ))<br/>{<br/>&nbsp;&nbsp;int i = (int) it.Current; // box and unbox here.<br/>&nbsp;&nbsp;Console.WriteLine( i.ToString( ) );<br/>}<br/>Instead, the foreach statement generates this construct for arrays:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;for ( int index = 0;<br/>&nbsp;&nbsp;index &lt; foo.Length;<br/>&nbsp;&nbsp;index++ )<br/>&nbsp;&nbsp;Console.WriteLine( foo[index].ToString( ));<br/>foreach always generates the best code. You don&#39;t need to remember which construct generates the most efficient looping construct: foreach and the compiler will do it for you.<br/>If efficiency isn&#39;t enough for you, consider language interop. Some folks in the world (yes, most of them use other programming languages) strongly believe that index variables start at 1, not 0. No matter how much we try, we won&#39;t break them of this habit. The .NET team tried. You have to write this kind of initialization in C# to get an array that starts at something other than 0:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;// Cr&#101;ate a single dimension array.<br/>// Its range is [ 1 .. 5 ]<br/>Array test = Array.Cr&#101;ateInstance( typeof( int ),<br/>new int[ ]{ 5 }, new int[ ]{ 1 });<br/>This code should be enough to make anybody cringe and just write arrays that start at 0. But some people are stubborn. Try as you might, they will start counting at 1. Luckily, this is one of those problems that you can foist off on the compiler. Iterate the test array using foreach:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;foreach( int j in test )<br/>&nbsp;&nbsp;Console.WriteLine ( j );<br/>The foreach statement knows how to check the upper and lower bounds on the array, so you don&#39;t have toand it&#39;s just as fast as a hand-coded for loop, no matter what different lower bound someone decides to use.<br/>foreach adds other language benefits for you. The loop variable is read-only: You can&#39;t replace the objects in a collection using foreach. Also, there is explicit casting to the correct type. If the collection contains the wrong type of objects, the iteration throws an exception.<br/>foreach gives you similar benefits for multidimensional arrays. Suppose that you are creating a chess board. You would write these two fragments:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;private Square[,] _theBoard = new Square[ 8, 8 ];<br/>// elsewh&#101;re in code:<br/>for ( int i = 0; i &lt; _theBoard.GetLength( 0 ); i++ )<br/>&nbsp;&nbsp;for( int j = 0; j &lt; _theBoard.GetLength( 1 ); j++ )<br/>&nbsp;&nbsp;&nbsp;&nbsp;_theBoard[ i, j ].PaintSquare( );<br/>Instead, you can simplify painting the board this way:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;foreach( Square sq in _theBoard )<br/>&nbsp;&nbsp;sq.PaintSquare( );<br/>The foreach statement generates the proper code to iterate across all dimensions in the array. If you make a 3D chessboard in the future, the foreach loop just works. The other loop needs modification:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;for ( int i = 0; i &lt; _theBoard.GetLength( 0 ); i++ )<br/>&nbsp;&nbsp;for( int j = 0; j &lt; _theBoard.GetLength( 1 ); j++ )<br/>&nbsp;&nbsp;&nbsp;&nbsp;for( int k = 0; k &lt; _theBoard.GetLength( 2 ); k++ )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_theBoard[ i, j, k ].PaintSquare( );<br/>In fact, the foreach loop would work on a multidimensional array that had different lower bounds in each direction. I don&#39;t want to write that kind of code, even as an example. But when someone else codes that kind of collection, foreach can handle it.<br/>foreach also gives you the flexibility to keep much of the code intact if you find later that you need to change the underlying data structure from an array. We started this discussion with a simple array:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;int [] foo = new int[100];<br/>Suppose that, at some later point, you realize that you need capabilities that are not easily handled by the array class. You can simply change the array to an ArrayList:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;// Set the initial size:<br/>ArrayList foo = new ArrayList( 100 );<br/>Any hand-coded for loops are broken:<br/>int sum = 0;<br/>for ( int index = 0;<br/>&nbsp;&nbsp;// won&#39;t compile: ArrayList uses Count, not Length<br/>&nbsp;&nbsp;index &lt; foo.Length;<br/>&nbsp;&nbsp;index++ )<br/>&nbsp;&nbsp;// won&#39;t compile: foo[ index ] is object, not int.<br/>&nbsp;&nbsp;sum += foo[ index ];<br/>However, the foreach loop compiles to different code that automatically casts each operand to the proper type. No changes are needed. It&#39;s not just changing to standard collections classes, eitherany collection type can be used with foreach.<br/>Users of your types can use foreach to iterate across members if you support the .NET environment&#39;s rules for a collection. For the foreach statement to consider it a collection type, a class must have one of a number of properties. The presence of a public GetEnumerator() method makes a collection class. Explicitly implementing the IEnumerable interface cr&#101;ates a collection type. Implementing the IEnumerator interface cr&#101;ates a collection type. foreach works with any of them.<br/>foreach has one added benefit regarding resource management. The IEnumerable interface contains one method: GetEnumerator(). The foreach statement on an enumerable type generates the following, with some optimizations:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;IEnumerator it = foo.GetEnumerator( ) as IEnumerator;<br/>using ( IDisposable disp = it as IDisposable )<br/>{<br/>&nbsp;&nbsp;while ( it.MoveNext( ))<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;int elem = ( int ) it.Current;<br/>&nbsp;&nbsp;&nbsp;&nbsp;sum += elem;<br/>&nbsp;&nbsp;}<br/>}<br/>The compiler automatically optimizes the code in the finally clause if it can determine for certain whether the enumerator implements IDisposable. But for you, it&#39;s more important to see that, no matter what, foreach generates correct code.<br/>foreach is a very versatile statement. It generates the right code for upper and lower bounds in arrays, iterates multidimensional arrays, coerces the operands into the proper type (using the most efficient construct), and, on top of that, generates the most efficient looping constructs. It&#39;s the best way to iterate collections. With it, you&#39;ll cr&#101;ate code that is more likely to last, and it&#39;s simpler for you to write in the first place. It&#39;s a small productivity improvement, but it adds up over time.</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;2.000&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;&#x31;&#xFF1A;&#x9009;&#x62E9;&#x66;&#x6F;&#x72;&#x65;&#x61;&#x63;&#x68;&#x5FAA;&#x73AF;</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;2.000&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;2.031&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card>
</wml>
