EffectiveC#11--选择foreach循环

1.C#的foreach语句可以为你的任何集合产生最好的迭代代码

   不推荐如下写法(?原因未明白 作者意思是阻碍jit边界检测)

int len = foo.Length;
 for ( int index = 0; index < len; index++ ) 
Console.WriteLine( foo[index].ToString( ));

2. 二维数组循环翻译人建议还是如下写法,而不是使用foreach写

private Square[,] _theBoard = new Square[ 8, 8 ]; 
for ( int i = 0; i < _theBoard.GetLength( 0 ); i++ )
 for( int j = 0; j < _theBoard.GetLength( 1 ); j++ ) 
       _theBoard[ i, j ].PaintSquare( );

  作者建议foreach

foreach( Square sq in _theBoard ) 
    sq.PaintSquare( );

3.注意数组与集合的区别。数组是一次性分配的连续内存,集合是可以动态添加与修改的,一般用链表来实现。

4.

posted @ 2015-10-15 22:52  王乐  阅读(183)  评论(0编辑  收藏  举报