IEnumerator和IEnumerable区别
2013-08-30 13:31
387 查看
IEnumerable接口和IEnumerator接口是.NET中非常重要的接口,二者有何区别?
1. 简单来说IEnumerable是一个声明式的接口,声明实现该接口的类就是“可迭代的enumerable”,但并没用说明如何实现迭代器(iterator).其代码实现为:
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
2. 而IEnumerator接口是实现式接口,它声明实现该接口的类就可以作为一个迭代器iterator.其代码实现为:
public interface IEnumerator
{
object Current { get; }
bool MoveNext();
void Reset();
}
3.一个collection要支持Foreach进行遍历,就必须实现IEnumerable,并一某种方式返回迭代器对象:IEnumerator.
foreach执行的顺序,foreach (Person p in peopleList),首先程序在peopleList执行public PeopleEnum GetEnumerator()生成一个用来迭代的类。然后执行“in”,在迭代类里面执行public bool MoveNext(),改变迭代类中position的位置。然后跳到执行Person,执行迭代类里面的public Person Current。返回的是当前person对象。
1. 简单来说IEnumerable是一个声明式的接口,声明实现该接口的类就是“可迭代的enumerable”,但并没用说明如何实现迭代器(iterator).其代码实现为:
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
2. 而IEnumerator接口是实现式接口,它声明实现该接口的类就可以作为一个迭代器iterator.其代码实现为:
public interface IEnumerator
{
object Current { get; }
bool MoveNext();
void Reset();
}
3.一个collection要支持Foreach进行遍历,就必须实现IEnumerable,并一某种方式返回迭代器对象:IEnumerator.
namespace ConsoleApplication1 { class Program { static void Main() { 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); Console.ReadKey(); } } public class Person //构造一个Person类 { public Person(string fName, string lName) { this.firstName = fName; this.lastName = lName; } public string firstName; public string lastName; } public class People : IEnumerable // People类是Person的集合类 { private Person[] _people; public People(Person[] pArray)//构造函数中将批量Person对象放入集合类的字段中 { _people = new Person[pArray.Length]; for (int i = 0; i < pArray.Length; i++) { _people[i] = pArray[i]; } } IEnumerator IEnumerable.GetEnumerator() //实现接口的GetEnumerator方法 { return (IEnumerator)GetEnumerator(); } public PeopleEnum GetEnumerator() //重写接口的GetEnumerator方法,自动调用该函数 { return new PeopleEnum(_people); } } public class PeopleEnum : IEnumerator //实现IEnumerator的类,用来实现迭代 { public Person[] _people; // Enumerators are positioned before the first element // until the first MoveNext() call. int position = -1; public PeopleEnum(Person[] list) { _people = list; } public bool MoveNext() { position++; return (position < _people.Length); } public void Reset() { position = -1; } object IEnumerator.Current //IEnumerator接口中的方法必须逐一实现 { get { return Current; } } public Person Current { get { try { return _people[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } }
foreach执行的顺序,foreach (Person p in peopleList),首先程序在peopleList执行public PeopleEnum GetEnumerator()生成一个用来迭代的类。然后执行“in”,在迭代类里面执行public bool MoveNext(),改变迭代类中position的位置。然后跳到执行Person,执行迭代类里面的public Person Current。返回的是当前person对象。
相关文章推荐
- IEnumerable和IEnumerator有什么区别?
- IEnumerable和IEnumerator有什么区别?
- IEnumerable与IEnumerator区别
- IEnumerator和IEnumerable的区别
- IEnumerable和 IEnumerator的区别
- IEnumerable、IEnumerator、IQuerabled的区别
- IEnumerable 与IEnumerator区别
- 关于迭代器中IEnumerable与IEnumerator的区别
- IEnumerable与IEnumerator在C#中的使用和区别
- IEnumerable和IEnumerator介绍和区别
- IEnumerable与IEnumerator区别
- IEnumerable和IEnumerator区别
- IEnumerable、IEnumerator、List、ArrayList、[]数组各各的区别
- IEnumerator与IEnumerable接口区别
- IEnumerable、GetEnumerator、IEnumerator之间的区别
- IEnumerable与IEnumerator区别
- IEnumerable与IEnumerator区别(带实例)
- C#_IEnumerable和IEnumerator的区别
- IEnumerable和IEnumerator区别有哪些
- C#中IEnumerable和IEnumerator区别