1、对象只要一个类型实现了IEnumerable接口就能遍历 2、IEnumerator是枚举器,一个接口类,实现MoveNext->Current->Reset 3、yield关键字是一个迭代器,相当于实现了IEnumerator枚举器 4、IEnumerable是可枚举类型,IEnumerator是枚举器
public class IEnumerableShow { ? ? ? ? public void Show() { ? ? ? ? ? ? int[] array = { 1, 2, 3, 4, 5 }; ? ? ? ? ? ? Student student = new Student { Id = 1 }; ? ? ? ? ? ? foreach (var item in array) { ? ? ? ? ? ? ? ? Console.WriteLine(); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? class Student:IEnumerable {? ? ? ? ? public int Id { get; set; } ? ? ? ? public IEnumerator GetEnumerator() { //返回一个枚举器 ? ? ? ? ? ? //yield return "Ant编程1"; ? ? ? ? ? ? //yield return "Ant编程2"; ? ? ? ? ? ? //yield return "Ant编程3"; ? ? ? ? ? ? string[] student = { "Ant编程1", "Ant编程2", "Ant编程3" }; ? ? ? ? ? ? return new StudentEnumerator(student); ? ? ? ? } ? ? } ? ? internal class StudentEnumerator : IEnumerator ? ? { ? ? ? ? string[] _student; ? ? ? ? int _position = -1; ? ? ? ? public StudentEnumerator(string[] student) { ? ? ? ? ? ? this._student = student; ? ? ? ? } ? ? ? ? public object Current { ? ? ? ? ? ? get { ? ? ? ? ? ? ? ? if (_position == -1) { ? ? ? ? ? ? ? ? ? ? throw new InvalidOperationException(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (_position>=_student.Length) { ? ? ? ? ? ? ? ? ? ? throw new InvalidOperationException(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return _student[_position]; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? public bool MoveNext() ? ? ? ? { ? ? ? ? ? ? if (_position<_student.Length-1) { ? ? ? ? ? ? ? ? _position++; ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? } ? ? ? ? ? ? else { ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? public void Reset() ? ? ? ? { ? ? ? ? ? ? _position = -1; ? ? ? ? } ? ? }
IEnumerator , IEnumerable 枚举器接口IEnumerator是枚举器的意思,IEnumerable是可枚举的意思。这两个都是个接口
foreach是一种语法糖,用来简化对可枚举元素的遍历代码。而被遍历的类通过实现IEnumerable接口和一个相关的IEnumerator枚举器来实现遍历功能。
public class MyList : IEnumerable { public int[] _data = new int[10] { 1, 5, 7, 9, 7, 8, 7, 8, 7, 4 }; public int this[int index] { get { return _data[index]; } } IEnumerator IEnumerable.GetEnumerator() { Debug.Log("foreach调用 GetEnumerator"); return new MIEnumtor(this); } }
public class MIEnumtor : IEnumerator { private MyList myList; private int index; public MIEnumtor(MyList my) { index = -1; myList = my; } public object Current { get { Debug.Log("foreach调用 Current"); return myList[index]; } } public bool MoveNext() { Debug.Log("foreach调用 MoveNext"); if (index < myList._data.Length - 1) { index++; return true; } index = -1; return false; } public void Reset() { } }
GetIEnumerator()负责获取枚举器。 MoveNext()负责让Current获取下一个值,并判断遍历是否结束。 Current负责返回当前指向的值。 Rest()负责重置枚举器的状态(在foreach中没有用到) 这些就是IEnumerable,IEnumerator的基本工作原理了。
MyList my = new MyList(); foreach (var item in my) { Debug.Log(item); }
等价于
MyList my = new MyList(); MIEnumtor mIEnumtor = my.GetEnumerator(); while (mIEnumtor.MoveNext()) { Debug.Log(mIEnumtor.Current); }
到此这篇关于C# IEnumerator枚举器的具体使用的文章就介绍到这了,更多相关C# IEnumerator枚举器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
查看更多关于C# IEnumerator枚举器的具体使用的详细内容...