C#接口之IEnumerable,IEnumerator
2015-10-19 14:59
831 查看
IEnumerable
截图来源于https://msdn.microsoft.com/zh-cn/library/system.collections.ienumerable.getenumerator.aspx
IEnumerable只包含一个抽象方法GetEnumerable(),它返回一个可用于循环访问集合的IEnumberator对象。我们可以通过定义foreach语句功能实现并支持非泛型方法的迭代。
在MSDN上给出了一个关于GetEnumerable()方法的例子:
如果我们将第23-42代码改写如下:
即不让People继承IEnumerable类,重写GetNumerator接口的部分也删除掉。那么编译器就会提示:
这是因为People类中没有实现GetNumerator()方法,所以People对象就不能返回一个IEnumerator对象,以至于foreach语句不能遍历Person集合。
IEnumerator
下面来介绍一下IEnumerator
截图来源于:https://msdn.microsoft.com/zh-cn/library/system.collections.ienumerator(v=vs.110).aspx
属性 描述
Current 获取集合中的当前元素
方法 描述
MoveNext 将枚举数推进到集合的下一个元素。
Reset 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
正是因为People类中没有实现GetNumerator()方法,所以People对象就不能返回一个IEnumerator对象,那么也就不能使用MoveNext方法,foreach也就不能实现遍历Person集合。
截图来源于https://msdn.microsoft.com/zh-cn/library/system.collections.ienumerable.getenumerator.aspx
IEnumerable只包含一个抽象方法GetEnumerable(),它返回一个可用于循环访问集合的IEnumberator对象。我们可以通过定义foreach语句功能实现并支持非泛型方法的迭代。
在MSDN上给出了一个关于GetEnumerable()方法的例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace TestInterface { public class Person { public Person(string fName,string lName) { this.firstName = fName; this.lastName = lName; } public string firstName; public string lastName; } public class People:IEnumerable//继承IEnumerable类,重写GetNumberable()方法 { private Person[] _peopele; public People(Person[] pArray) { _peopele = new Person[pArray.Length]; for (int i = 0; i < pArray.Length; i++) _peopele[i] = pArray[i]; } IEnumerator IEnumerable.GetEnumerator()//实现GetEnumerable接口 { return (IEnumerator) GetEnumerator();//调用重写的GetEnumerator方法 } public PeopleEnum GetEnumerator() { return new PeopleEnum(_peopele);//返回一个PeopleEnum类型的对象,PeopleEnum继承了IEnumerator类 } } public class PeopleEnum:IEnumerator { public Person[] _people; int position = -1; /// <summary> /// 构造函数 /// </summary> /// <param name="list"> /// _people指针指向list数组 /// </param> public PeopleEnum(Person[] list) { _people = list; } /// <summary> ///将枚举数推进到集合的下一个元素。 /// </summary> /// <returns> /// 成功返回true,失败返回false /// </returns> public bool MoveNext() { position++; return (position < _people.Length); } /// <summary> /// 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。 /// </summary> public void Reset() { position = -1; } /// <summary> /// 获取当前位置的对象 /// </summary> object IEnumerator.Current { get { return Current; } } /// <summary> /// 实现接口方法 /// </summary> public Person Current { get { try { return _people[position]; } catch(IndexOutOfRangeException) { throw new InvalidOperationException(); } } } } class Program { static void Main(string[] args) { Person[] peopleArray = new Person[3] { new Person("John","Smith"), new Person("Jim","Johnson"), new Person("Sue","Rabon"), }; People peopleList = new People(peopleArray);//初始化对象集合 foreach (Person p in peopleList) Console.WriteLine(p.firstName + " " + p.lastName); } } }
如果我们将第23-42代码改写如下:
public class People//继承IEnumerable类,重写GetNumberable()方法 { private Person[] _peopele; public People(Person[] pArray) { _peopele = new Person[pArray.Length]; for (int i = 0; i < pArray.Length; i++) _peopele[i] = pArray[i]; } }
即不让People继承IEnumerable类,重写GetNumerator接口的部分也删除掉。那么编译器就会提示:
这是因为People类中没有实现GetNumerator()方法,所以People对象就不能返回一个IEnumerator对象,以至于foreach语句不能遍历Person集合。
IEnumerator
下面来介绍一下IEnumerator
截图来源于:https://msdn.microsoft.com/zh-cn/library/system.collections.ienumerator(v=vs.110).aspx
属性 描述
Current 获取集合中的当前元素
方法 描述
MoveNext 将枚举数推进到集合的下一个元素。
Reset 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
正是因为People类中没有实现GetNumerator()方法,所以People对象就不能返回一个IEnumerator对象,那么也就不能使用MoveNext方法,foreach也就不能实现遍历Person集合。
相关文章推荐
- C#项目实战-实现一个多线程端口扫描器
- 【工作记录0014】C#"该行已经属于另一个表"解决方法
- C# Winform 界面中各控件随着窗口大小变化
- 修饰模式(Decorator结构化)C#简单的例子
- C#中 ListView控件行高的巧妙设置
- C#语法中一个问号(?)与两个问号(??)有什么区别
- C#
- C#中使用ListView动态添加数据不闪烁
- C#文件写入
- C#时间戳
- C#委托的介绍(delegate、Action、Func、predicate)
- C#学习日记26---接口(interface)类型
- 使用C#和Thrift来访问Hbase实例
- C#基础巩固(2)-Linq To XML创建XML
- C# 5.0 与 .Net 4.5 学习(一)Race Conditions资源竞争
- C#可以自动在后台为属性创建字段
- C#.NET里面抽象类和接口有什么区别
- 利用序列化的方式实现C#深复制和浅复制
- C#线程并发执行的实例[转]
- C# 可否对内存进行直接的操作?