1、首先看一个简单的例子
<span style="line-height: 21px; color: blue;"><span style="font-family: Consolas;">int</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">[] myArray = { 1, 32, 43, 343 }; </span><span style="line-height: 21px; color: rgb(43, 145, 175);">IEnumerator </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">myie = myArray.GetEnumerator(); myie.Reset(); </span><span style="line-height: 21px; color: blue;">while </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">(myie.MoveNext()) { </span><span style="line-height: 21px; color: blue;">int </span><span style="line-height: 21px;">i = (</span><span style="line-height: 21px; color: blue;">int</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">)myie.Current; </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Console</span><span style="line-height: 21px;">.WriteLine(</span><span style="line-height: 21px; color: rgb(163, 21, 21);">"Value: {0}"</span></span><span style="line-height: 21px;"><span style="font-family: Consolas;">, i); }</span></span>
<span style="line-height: 21px;"><span style="font-family: 微软雅黑;">相信很多人都不会像上面这样去遍历myArray这个数组,通常我们这样会这样做</span></span>
<span style="line-height: 21px;"> </span><span style="font-family: Consolas;"><span style="line-height: 21px; color: blue;">foreach </span><span style="line-height: 21px;">(</span><span style="line-height: 21px; color: blue;">int </span><span style="line-height: 21px;">item </span><span style="line-height: 21px; color: blue;">in </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">myArray) </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Console</span><span style="line-height: 21px;">.WriteLine(item.ToString());</span></span>
<span style="font-family: Consolas;"><span style="line-height: 21px;"></span></span>
<span style="font-family: 微软雅黑;">在大部分的情况下,很多人会使用for和foreach来遍历数组,而对于上面的语法却用的很少,但是对foreach的具体来历还很模糊!</span>
<span style="font-family: 微软雅黑;"></span>
<span style="line-height: 21px;"><span style="font-family: 微软雅黑;"></span></span>
<span style="font-family: 微软雅黑; background-color: rgb(75, 172, 198);"><strong>2、理解foreach</strong></span>
大家都知道要实现foreach的必须要实现IEnumerable和IEnumerator的接口,只有实现了它们,才能实现遍历,所以要讲foreach的来历,必须要把那两个接口给搞清楚点!
这边也想说明一点的是:如果对这两个接口有了一定的了解后,只要实现那个GetEnumerator方法即可,而不需要实现于IEnumerable接口,这只是针对对这两个接口有一定了解的朋友!
2.1 IEnumerable
具体的作用:就是使实现这个接口的对象成为可枚举类型。
IEnumerable接口包含一个GetEnumerator方法,返回值为IEnumerator的类型!代码如下:
<span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;"> </span><span style="line-height: 21px; color: blue; background-color: white;">public class </span><span style="line-height: 21px; color: rgb(43, 145, 175); background-color: white;">MyColors </span><span style="line-height: 21px; color: black; background-color: white;">: </span></span><span style="line-height: 21px; color: rgb(43, 145, 175); background-color: white;"><span style="font-family: Consolas;">IEnumerable </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">{ </span><span style="line-height: 21px; color: blue; background-color: white;">string</span><span style="line-height: 21px; color: black; background-color: white;">[] colors = { </span><span style="line-height: 21px; color: rgb(163, 21, 21); background-color: white;">"Red"</span><span style="line-height: 21px; color: black; background-color: white;">, </span><span style="line-height: 21px; color: rgb(163, 21, 21); background-color: white;">"Yellow"</span><span style="line-height: 21px; color: black; background-color: white;">, </span><span style="line-height: 21px; color: rgb(163, 21, 21); background-color: white;">"Biue" </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">}; </span><span style="line-height: 21px; color: blue; background-color: white;">public </span><span style="line-height: 21px; color: rgb(43, 145, 175); background-color: white;">IEnumerator </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">GetEnumerator() { </span><span style="line-height: 21px; color: blue; background-color: white;">return </span></span><span style="line-height: 21px; color: black; background-color: white;"><span style="font-family: Consolas;">colors.GetEnumerator(); } }</span></span>那么我们在客户端进行调用的时候就可以这样做了!
<span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;"> </span><span style="line-height: 21px; color: rgb(43, 145, 175); background-color: white;">MyColors </span><span style="line-height: 21px; color: black; background-color: white;">colors = </span><span style="line-height: 21px; color: blue; background-color: white;">new </span><span style="line-height: 21px; color: rgb(43, 145, 175); background-color: white;">MyColors</span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">(); </span><span style="line-height: 21px; color: blue; background-color: white;">foreach </span><span style="line-height: 21px; color: black; background-color: white;">(</span><span style="line-height: 21px; color: blue; background-color: white;">string </span><span style="line-height: 21px; color: black; background-color: white;">item </span><span style="line-height: 21px; color: blue; background-color: white;">in </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">colors) </span><span style="line-height: 21px; color: rgb(43, 145, 175); background-color: white;">Console</span><span style="line-height: 21px; color: black; background-color: white;">.WriteLine(item);</span></span><span style="font-family: 微软雅黑;">这样就能使用实现这个接口的对象进行foreach遍历了,就是这么简单的一个列子,这边可能有的人会有疑问,我们就实现了IEnumerable接口但却没实现IEnumerator接口,可是我们不是说只有实现了这两个接口才可以进行foreach遍历吗?可以这样说当使用forach进行遍历的时候,编译器会到这个对象的类中去寻找GetEnumerator方法,找到这个方法后返回这个一个IEnumerator的对象(枚举数),而我这边返回的是“colors.GetEnumerator()”,是一个数组对象调用它本身中的“GetEnumerator”方法,所以说数组本身就实现了IEnumerator接口,那么两个接口都实现了,不就好实现foreach遍历了,其实在实现遍历枚举数的时候编译器会自动去调用数组中实现枚举数的类中的方法。</span><span style="font-family: 微软雅黑;"></span><span style="font-family: 微软雅黑; background-color: rgb(204, 180, 0);">2.2 IEnumerator</span>接口的作用:实现可枚举数,首先看一下接口的定义:
包含一个属性两个方法
MoveNext → 把当前的项移动到下一项(类似于索引值),返回一个bool值,这个bool值用来检查当前项是否超出了枚举数的范围!
Current → 获取当前项的值,返回一个object的类型(这边会涉及到装箱和拆箱的问题 → 后面讲泛型的时候会涉及到)!
Reset → 顾名思义也就是把一些值恢复为默认值,比如把当前项恢复到默认状态值!
<span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;"> </span><span style="line-height: 21px; color: blue; background-color: white;">public class </span><span style="line-height: 21px; color: rgb(43, 145, 175); background-color: white;">MyIEnumerator </span><span style="line-height: 21px; color: black; background-color: white;">: </span></span><span style="line-height: 21px; color: rgb(43, 145, 175); background-color: white;"><span style="font-family: Consolas;">IEnumerator </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">{ </span><span style="line-height: 21px; color: blue; background-color: white;">public </span><span style="line-height: 21px; color: black; background-color: white;">MyIEnumerator(</span><span style="line-height: 21px; color: blue; background-color: white;">string</span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">[] colors) { </span><span style="line-height: 21px; color: blue; background-color: white;">this</span><span style="line-height: 21px; color: black; background-color: white;">.colors = </span><span style="line-height: 21px; color: blue; background-color: white;">new string</span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">[colors.Length]; </span><span style="line-height: 21px; color: blue; background-color: white;">for </span><span style="line-height: 21px; color: black; background-color: white;">(</span><span style="line-height: 21px; color: blue; background-color: white;">int </span><span style="line-height: 21px; color: black; background-color: white;">i = 0; i < </span><span style="line-height: 21px; color: blue; background-color: white;">this</span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">.colors.Length; i++) </span><span style="line-height: 21px; color: blue; background-color: white;">this</span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">.colors[i] = colors[i]; } </span><span style="line-height: 21px; color: blue; background-color: white;">string</span><span style="line-height: 21px; color: black; background-color: white;">[] colors; </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: green; background-color: white;">//定义一个数组,用来存储数据 </span><span style="line-height: 21px; color: blue; background-color: white;">int </span><span style="line-height: 21px; color: black; background-color: white;">position = -1; </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: green; background-color: white;">//定义当前项的默认值,也就是索引值,一开始认识数组的索引从“0”开始, //怎么默认设置他为“-1”呢,最后才想明白,这样设置是合情合理的! </span><span style="line-height: 21px; color: blue; background-color: white;">public object </span><span style="line-height: 21px; color: black; background-color: white;">Current </span></span><span style="line-height: 21px; color: green; background-color: white;"><span style="font-family: Consolas;">//根据当前项获取相应的值 </span></span><span style="line-height: 21px; color: black; background-color: white;"><span style="font-family: Consolas;">{ </span></span><span style="line-height: 21px; color: blue; background-color: white;"><span style="font-family: Consolas;">get </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">{ </span><span style="line-height: 21px; color: blue; background-color: white;">return </span><span style="line-height: 21px; color: black; background-color: white;">colors[position]; </span></span><span style="line-height: 21px; color: green; background-color: white;"><span style="font-family: Consolas;">//返回当前项的值,但是会做一个装箱的操作! </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">} } </span><span style="line-height: 21px; color: blue; background-color: white;">public bool </span><span style="line-height: 21px; color: black; background-color: white;">MoveNext() </span></span><span style="line-height: 21px; color: green; background-color: white;"><span style="font-family: Consolas;">//移动到下一项 </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">{ </span><span style="line-height: 21px; color: blue; background-color: white;">if </span><span style="line-height: 21px; color: black; background-color: white;">(position < colors.Length - 1) </span></span><span style="line-height: 21px; color: green; background-color: white;"><span style="font-family: Consolas;">//这就是设置默认值为-1的根据 </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">{ position++; </span><span style="line-height: 21px; color: blue; background-color: white;">return true</span></span><span style="line-height: 21px; color: black; background-color: white;"><span style="font-family: Consolas;">; } </span></span><span style="line-height: 21px; color: blue; background-color: white;"><span style="font-family: Consolas;">else </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">{ </span><span style="line-height: 21px; color: blue; background-color: white;">return false</span></span><span style="line-height: 21px; color: black; background-color: white;"><span style="font-family: Consolas;">; } } </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: green; background-color: white;">//重置当前项的值,恢复为默认值 </span><span style="line-height: 21px; color: blue; background-color: white;">public void </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">Reset() { </span><span style="line-height: 21px; color: blue; background-color: white;">this</span></span><span style="line-height: 21px; color: black; background-color: white;"><span style="font-family: Consolas;">.position = -1; } }</span></span><span style="line-height: 21px; color: black; background-color: white;"><span style="font-family: Consolas;"></span></span><span style="font-family: 微软雅黑;">上面讲到的IEnumerable接口中GetEnumerator方法是获取要遍历的枚举数,在我们没有创建自己的遍历枚举数的类时,我们使用的是Array的遍历枚举数的方法(关于数组的可枚举类型和枚举数会在下一篇讲到),但这个有的时候不一定适合我们,我们需要为自己定制一个更合适的,所以我们要创建自己的枚举数类(也就是上面的代码),把第三点和第四点的代码合并起来(改动一点代码),如下:</span><span style="line-height: 21px; color: black; background-color: white;"><span style="font-family: Consolas;"></span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;"> </span><span style="line-height: 21px; color: blue; background-color: white;">public class </span><span style="line-height: 21px; color: rgb(43, 145, 175); background-color: white;">MyColors </span></span><span style="line-height: 21px; color: green; background-color: white;"><span style="font-family: Consolas;">//: IEnumerable </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">{ </span><span style="line-height: 21px; color: blue; background-color: white;">string</span><span style="line-height: 21px; color: black; background-color: white;">[] colors = { </span><span style="line-height: 21px; color: rgb(163, 21, 21); background-color: white;">"Red"</span><span style="line-height: 21px; color: black; background-color: white;">, </span><span style="line-height: 21px; color: rgb(163, 21, 21); background-color: white;">"Yellow"</span><span style="line-height: 21px; color: black; background-color: white;">, </span><span style="line-height: 21px; color: rgb(163, 21, 21); background-color: white;">"Biue" </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">}; </span><span style="line-height: 21px; color: blue; background-color: white;">public </span><span style="line-height: 21px; color: rgb(43, 145, 175); background-color: white;">IEnumerator </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: black; background-color: white;">GetEnumerator() { </span><span style="line-height: 21px; color: blue; background-color: white;">return <span style="background-color: rgb(221, 132, 132);">new </span></span><span style="line-height: 21px; color: rgb(43, 145, 175); background-color: white;"><span style="background-color: rgb(221, 132, 132);">MyIEnumerator</span></span></span><span style="line-height: 21px; color: black; background-color: white;"><span style="font-family: Consolas;"><span style="background-color: rgb(221, 132, 132);">(colors);</span> } }</span></span>
<strong></strong>
<strong><span style="background-color: rgb(75, 172, 198);">3、关于可枚举类型和枚举数</span></strong>
<strong></strong>
①可枚举类型 → 实现IEnumerable接口,可以不需要直接实现这个接口,但必须有个GetEnumerator方法,返回值类型必须为IEnumerator类型,也就是第四点最后一段代码中接口注释的那种写法!
②枚举数 → 实现IEnumerator接口,实现全部方法,首先是调用GetEnumerator返回一个类型为IEnumerator的枚举数,然后编译器会隐式的调用实现IEnumerator类中的方法和属性!
总结:所以实现foreach遍历,必须达到上面的两种条件才能进行遍历对象,他们可以写在一起也可以分开,最好是分开,进行职责分离,一个类干一件事总归是好事!也满足面向对象的单一指责设计原则。
下面的代码示例演示如何实现自定义集合的 IEnumerable 和 IEnumerator 接口(代码来自MSDN)
<span style="line-height: 21px; color: blue;"><span style="font-family: Consolas;">using </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">System; </span><span style="line-height: 21px; color: blue;">using </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">System.Collections; </span><span style="line-height: 21px; color: blue;">using </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">System.Collections.Generic; </span><span style="line-height: 21px; color: blue;">using </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">System.Linq; </span><span style="line-height: 21px; color: blue;">using </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">System.Text; </span><span style="line-height: 21px; color: blue;">using </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">System.Threading.Tasks; </span><span style="line-height: 21px; color: blue;">namespace </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">ConsoleApplication1 { </span><span style="line-height: 21px; color: blue;">public class </span></span><span style="line-height: 21px; color: rgb(43, 145, 175);"><span style="font-family: Consolas;">Person </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">{ </span><span style="line-height: 21px; color: blue;">public </span><span style="line-height: 21px;">Person(</span><span style="line-height: 21px; color: blue;">string </span><span style="line-height: 21px;">fName, </span><span style="line-height: 21px; color: blue;">string </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">lName) { </span><span style="line-height: 21px; color: blue;">this</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">.firstName = fName; </span><span style="line-height: 21px; color: blue;">this</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">.lastName = lName; } </span><span style="line-height: 21px; color: blue;">public string </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">firstName; </span><span style="line-height: 21px; color: blue;">public string </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">lastName; } </span><span style="line-height: 21px; color: blue;">public class </span><span style="line-height: 21px; color: rgb(43, 145, 175);">People </span><span style="line-height: 21px;">: </span></span><span style="line-height: 21px; color: rgb(43, 145, 175);"><span style="font-family: Consolas;">IEnumerable </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">{ </span><span style="line-height: 21px; color: blue;">private </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">[] _people; </span><span style="line-height: 21px; color: blue;">public </span><span style="line-height: 21px;">People(</span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">[] pArray) { _people = </span><span style="line-height: 21px; color: blue;">new </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">[pArray.Length]; </span><span style="line-height: 21px; color: blue;">for </span><span style="line-height: 21px;">(</span><span style="line-height: 21px; color: blue;">int </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">i = 0; i < pArray.Length; i++) { _people[i] = pArray[i]; } } </span><span style="line-height: 21px; color: rgb(43, 145, 175);">IEnumerator IEnumerable</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">.GetEnumerator() { </span><span style="line-height: 21px; color: blue;">return </span><span style="line-height: 21px;">(</span><span style="line-height: 21px; color: rgb(43, 145, 175);">IEnumerator</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">)GetEnumerator(); } </span><span style="line-height: 21px; color: blue;">public </span><span style="line-height: 21px; color: rgb(43, 145, 175);">PeopleEnum </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">GetEnumerator() { </span><span style="line-height: 21px; color: blue;">return new </span><span style="line-height: 21px; color: rgb(43, 145, 175);">PeopleEnum</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">(_people); } } </span><span style="line-height: 21px; color: blue;">public class </span><span style="line-height: 21px; color: rgb(43, 145, 175);">PeopleEnum </span><span style="line-height: 21px;">: </span></span><span style="line-height: 21px; color: rgb(43, 145, 175);"><span style="font-family: Consolas;">IEnumerator </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">{ </span><span style="line-height: 21px; color: blue;">public </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person</span></span><span style="line-height: 21px;"><span style="font-family: Consolas;">[] _people; </span></span><span style="font-family: Consolas;"><span style="line-height: 21px; color: green;">// Enumerators are positioned before the first element // until the first MoveNext() call. </span><span style="line-height: 21px; color: blue;">int </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">position = -1; </span><span style="line-height: 21px; color: blue;">public </span><span style="line-height: 21px;">PeopleEnum(</span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">[] list) { _people = list; } </span><span style="line-height: 21px; color: blue;">public bool </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">MoveNext() { position++; </span><span style="line-height: 21px; color: blue;">return </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">(position < _people.Length); } </span><span style="line-height: 21px; color: blue;">public void </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">Reset() { position = -1; } </span><span style="line-height: 21px; color: blue;">object </span><span style="line-height: 21px; color: rgb(43, 145, 175);">IEnumerator</span></span><span style="line-height: 21px;"><span style="font-family: Consolas;">.Current { </span></span><span style="line-height: 21px; color: blue;"><span style="font-family: Consolas;">get </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">{ </span><span style="line-height: 21px; color: blue;">return </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">Current; } } </span><span style="line-height: 21px; color: blue;">public </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person </span></span><span style="line-height: 21px;"><span style="font-family: Consolas;">Current { </span></span><span style="line-height: 21px; color: blue;"><span style="font-family: Consolas;">get </span></span><span style="line-height: 21px;"><span style="font-family: Consolas;">{ </span></span><span style="line-height: 21px; color: blue;"><span style="font-family: Consolas;">try </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">{ </span><span style="line-height: 21px; color: blue;">return </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">_people[position]; } </span><span style="line-height: 21px; color: blue;">catch </span><span style="line-height: 21px;">(</span><span style="line-height: 21px; color: rgb(43, 145, 175);">IndexOutOfRangeException</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">) { </span><span style="line-height: 21px; color: blue;">throw new </span><span style="line-height: 21px; color: rgb(43, 145, 175);">InvalidOperationException</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">(); } } } } </span><span style="line-height: 21px; color: blue;">class </span></span><span style="line-height: 21px; color: rgb(43, 145, 175);"><span style="font-family: Consolas;">Program </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">{ </span><span style="line-height: 21px; color: blue;">static void </span><span style="line-height: 21px;">Main(</span><span style="line-height: 21px; color: blue;">string</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">[] args) { </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person</span><span style="line-height: 21px;">[] peopleArray = </span><span style="line-height: 21px; color: blue;">new </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">[3] { </span><span style="line-height: 21px; color: blue;">new </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person</span><span style="line-height: 21px;">(</span><span style="line-height: 21px; color: rgb(163, 21, 21);">"John"</span><span style="line-height: 21px;">, </span><span style="line-height: 21px; color: rgb(163, 21, 21);">"Smith"</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">), </span><span style="line-height: 21px; color: blue;">new </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person</span><span style="line-height: 21px;">(</span><span style="line-height: 21px; color: rgb(163, 21, 21);">"Jim"</span><span style="line-height: 21px;">, </span><span style="line-height: 21px; color: rgb(163, 21, 21);">"Johnson"</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">), </span><span style="line-height: 21px; color: blue;">new </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person</span><span style="line-height: 21px;">(</span><span style="line-height: 21px; color: rgb(163, 21, 21);">"Sue"</span><span style="line-height: 21px;">, </span><span style="line-height: 21px; color: rgb(163, 21, 21);">"Rabon"</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">), }; </span><span style="line-height: 21px; color: rgb(43, 145, 175);">People </span><span style="line-height: 21px;">peopleList = </span><span style="line-height: 21px; color: blue;">new </span><span style="line-height: 21px; color: rgb(43, 145, 175);">People</span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">(peopleArray); </span><span style="line-height: 21px; color: blue;">foreach </span><span style="line-height: 21px;">(</span><span style="line-height: 21px; color: rgb(43, 145, 175);">Person </span><span style="line-height: 21px;">p </span><span style="line-height: 21px; color: blue;">in </span></span><span style="font-family: Consolas;"><span style="line-height: 21px;">peopleList) </span><span style="line-height: 21px; color: rgb(43, 145, 175);">Console</span><span style="line-height: 21px;">.WriteLine(p.firstName + </span><span style="line-height: 21px; color: rgb(163, 21, 21);">" " </span></span><span style="line-height: 21px;"><span style="font-family: Consolas;">+ p.lastName); } } }</span> </span>
无图无真相
我并非标题党,下面介绍IList,ICollection,IQueryable
1、IList 是 ICollection 接口的子代,并且是所有非泛型列表的基接口。IList 实现有三种类别:只读、固定大小和可变大小。无法修改只读 IList。固定大小的 IList 不允许添加或移除元素,但允许修改现有元素。可变大小的 IList 允许添加、移除和修改元素。
2、ICollection 接口是 System.Collections 命名空间中类的基接口。ICollection 接口扩展 IEnumerable;IDictionary 和 IList 则是扩展 ICollection 的更为专用的接口。 IDictionary 实现是键/值对的集合,如 Hashtable 类。 IList 实现是值的集合,其成员可通过索引访问,如 ArrayList 类。 某些集合(如 Queue 类和 Stack 类)限制对其元素的访问,它们直接实现 ICollection 接口。 如果 IDictionary 接口和 IList 接口都不能满足所需集合的要求,则从 ICollection 接口派生新集合类以提高灵活性。定义所有非泛型集合的大小、枚举器和同步方法。
3、IQueryable 提供对未指定数据类型的特定数据源的查询进行计算的功能,IQueryable 接口由查询提供程序实现。 该接口只能由同时实现 IQueryable(Of T) 的提供程序实现。 如果该提供程序不实现 IQueryable(Of T),则无法对提供程序数据源使用标准查询运算符。 IQueryable 接口继承 IEnumerable 接口,以便在前者表示一个查询时可以枚举该查询的结果。 枚举强制执行与 IQueryable 对象关联的表达式树。 “执行表达式树”的定义是查询提供程序所特有的。 例如,它可能涉及将表达式树转换为适用于基础数据源的查询语言。 在调用 Execute 方法时将执行不返回可枚举结果的查询。