摘要: 使用foreach可以方便地遍历集合,但是这些集合都是系统内置的,如何构造自定义的集合也可以用foreach来遍历呢? 这里涉及两个接口,IEnumerable和IEnumerator。通过实现这两个接口,就可以了。 下面给出一个简单的Demo 1using System; 2using System.Collections.Generic; 3using System.Linq; ... 阅读全文
posted @ 2008-06-27 16:23 sunlei 阅读(274) 评论(0) 推荐(0) 编辑
摘要: 在.net环境里,如果需要遍历集合的话,常用的就是foreach,根据Effective C#所说,也是最佳的实践方式。 下面首先比较各种遍历集合的方式: 1int [] foo = new int[100]; 2 3// Loop 1: 4foreach ( int i in foo) 5 Console.WriteLine( i.ToString( )); 6 7// Loop... 阅读全文
posted @ 2008-06-27 16:03 sunlei 阅读(820) 评论(0) 推荐(1) 编辑