IEnumerable、IEnumerator、ICollection、IList、List的继承关系及简单使用

IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach语句功能的实现并支持非泛型方法的简单的迭代,IEnumerable和IEnumerable<T>接口是.NET Framework中最基本的集合访问器。它定义了一组扩展方法,用来对数据集合中的元素进行遍历、过滤、排序、搜索等操作。

IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。IEnumerator对象有什么呢?它是一个真正的集合访器,没有它,就不能使用foreach语句遍历集合或数组,因为只有IEnumerator对象才能访问集合中的项,假如连集合中的项都访问不了,那么进行集合的循环遍历是不可能的事情了。

1、IEnumerator接口

提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型。

IEnumerator<T>:继承自IEnumerator,有Current属性,返回的是T类型。

1     public interface IEnumerator
2     {
3       bool MoveNext(); //将游标的内部位置向前移动
4       object Current{get;} //获取当前的项(只读属性)
5       void Reset(); //将游标重置到第一个成员前面
6     }

2、IEnumerable接口

暴露一个IEnumerator,支持在普通集合中的遍历。

IEnumerable<T>:继承自IEnumerable,暴露一个IEnumerator<T>,支持在泛型集合中遍历。

 1     // 摘要:
 2     //     公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。
 3     //
 4     // 类型参数:
 5     //   T:
 6     //     要枚举的对象的类型。
 7     [TypeDependency("System.SZArrayHelper")]
 8     public interface IEnumerable<out T> : IEnumerable
 9     {
10         // 摘要:
11         //     返回一个循环访问集合的枚举器。
12         //
13         // 返回结果:
14         //     可用于循环访问集合的 System.Collections.Generic.IEnumerator<T>。
15         IEnumerator<T> GetEnumerator();
16     }

可以看到,GetEnumerator方法返回对另一个接口System.Collections.IEnumerator的引用。这个接口提供了基础设施,调用方可以用来移动IEnumerable兼容容器包含的内部对象。

3、ICollection接口

同时继承IEnumerable<T>和IEnumerable两个接口

1     // 摘要:
2     //     定义操作泛型集合的方法。
3     //
4     // 类型参数:
5     //   T:
6     //     集合中元素的类型。
7     [TypeDependency("System.SZArrayHelper")]
8     public interface ICollection<T> : IEnumerable<T>, IEnumerable

4、IList接口

1     public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable

5、List的定义

1     public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

实现IEnumerable接口

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Collections;
 6 namespace IEnumeratorSample
 7 {
 8     class Person : IEnumerable
 9     {
10         public string Name;//定义Person的名字
11         public string Age;//定义Person的年龄
12 
13         public Person(string name, string age)//为Person初始化(构造函数)
14         {
15             Name = name;
16             Age = age;
17         }
18 
19         private Person[] per;
20 
21         public Person(Person[] array)//重载构造函数,迭代对象
22         {
23             per = new Person[array.Length];//创建对象
24             for (int i = 0; i < array.Length; i++)//遍历初始化对象
25             {
26                 per[i] = array[i];//数组赋值
27             }
28         }
29 
30         public IEnumerator GetEnumerator()//实现接口
31         {
32             return new PersonEnum(per);
33         }
34     }
35 
36     class PersonEnum : IEnumerator//实现foreach语句内部,并派生
37     {
38         public Person[] _per;//实现数组
39         int position = -1;//设置“指针”
40 
41         public PersonEnum(Person[] list)
42         {
43             _per = list;//实现list
44         }
45 
46         public bool MoveNext()//实现向前移动
47         {
48             position++;//位置增加
49             return (position < _per.Length);//返回布尔值
50         }
51 
52         public void Reset()//位置重置
53         {
54             position = -1;//重置指针为-1
55         }
56 
57         public object Current//实现接口方法
58         {
59             get
60             {
61                 try
62                 {
63                     return _per[position];//返回对象
64                 }
65                 catch (IndexOutOfRangeException)//捕获异常
66                 {
67                     throw new InvalidOperationException();//抛出异常信息
68                 }
69             }
70         }
71     }
72 
73     class Program
74     {
75         static void Main(string[] args)
76         {
77             Person[] per = new Person[2]
78             {
79                 new Person("Jack","21"),
80                 new Person("David","24"),
81             };
82             Person personlist = new Person(per);
83             //遍历对象
84             foreach (Person p in personlist)
85             {
86                 Console.WriteLine("Name is " + p.Name + " and Age is " + p.Age);
87             }
88             Console.ReadKey();
89         }
90     }
91 }

原文链接:http://www.studyofnet.com/news/452.html

相关博客1:http://blog.csdn.net/callmeback/article/details/8295648

相关博客2:http://www.cnblogs.com/shaosks/archive/2011/09/27/2193270.html

原文地址:https://www.cnblogs.com/makesense/p/6308554.html