IEnumerable, IEnumerator接口

时间:2023-03-09 17:25:47
IEnumerable, IEnumerator接口

IEnumerable接口

//     Exposes the enumerator, which supports a simple iteration over a non-generic collection.
public interface IEnumerable
{// Returns an enumerator that iterates through a collection.
IEnumerator GetEnumerator();
}

IEnumerator接口

//     Supports a simple iteration over a nongeneric collection.
public interface IEnumerator
{// Gets the current element in the collection.
object Current { get; }
// Advances the enumerator to the next element of the collection.
bool MoveNext();
// Sets the enumerator to its initial position, which is before the first element in the collection.
void Reset();
}

示例1:

    class MyClass : IEnumerable
{
private int[] sources; public MyClass()
{
this.sources = new int[] { , , , , , , , , , , , , , , , , , , };
} public int this[int index]
{
get { return this.sources[index]; }
} public int Length { get { return this.sources.Length; } } public IEnumerator GetEnumerator()
{
return new MyClassEnumerator(this);
}
} class MyClassEnumerator : IEnumerator
{
private MyClass myobject;
private int index = -; public MyClassEnumerator(MyClass myobject)
{
this.myobject = myobject;
} public object Current
{
get { return this.myobject[index]; }
} public bool MoveNext()
{
if (this.index < this.myobject.Length - )
{
this.index++;
return true;
} return false;
} public void Reset() { this.index = -; }
} static void Main(string[] args)
{
MyClass myobject = new MyClass(); foreach (var item in myobject)
{
Console.WriteLine(item);
} return;
}

示例2: 用yield关键字简化实现.

yield是一个语法糖, 编译器会重新翻译这段代码, 实际上是返回了一个实现了IEnumerator 的对象, 每一次的yield循环对应MoveNext, return this[i]对应于Current属性.

参考:http://www.cnblogs.com/artech/archive/2013/04/14/yield-in-wcf-02.html

    class MyClass : IEnumerable
{
private int[] sources; public MyClass()
{
this.sources = new int[] { , , , , , , , , , , , , , , , , , , };
} public int this[int index]
{
get { return this.sources[index]; }
} public int Length { get { return this.sources.Length; } } public IEnumerator GetEnumerator()
{
//return new MyClassEnumerator(this);
for (int i = ; i < this.Length; i++)
{
yield return this[i];
}
}
}

示例3: 一个对象同时实现了IEnumerable, IEnumerator. 这个实例仅供实验研究, 不推荐实际中应用.

提问: 为什么还要实现IEnumerable, 直接实现了IEnumerator就可以foreach不行吗?

回答: 请把枚举器想象成类似指针的功能, 可以遍历指向的集合.但是一个集合对象最好允许有多个枚举器, 如果集合对象直接实现IEnumerator, 集合对象和枚举器就耦合在一起,一个集合对象就只能有一个枚举器了,也就是它本身就是自己的枚举器。就算是不考虑支持多种枚举器,只考虑对一个枚举器的同时进行多次枚举,只实现IEnumerable也是做不到。

     class MyClass : IEnumerable, IEnumerator
{
private int[] sources;
private int index = -; public MyClass()
{
this.sources = new int[] { , , , , , , , , , , , , , , , , , , };
} public int this[int index]
{
get { return this.sources[index]; }
} public int Length { get { return this.sources.Length; } } public IEnumerator GetEnumerator()
{
//return new MyClassEnumerator(this);
for (int i = ; i < this.Length; i++)
{
yield return this[i];
}
} public object Current
{
get { return this[index]; }
} public bool MoveNext()
{
if (this.index < this.Length - )
{
this.index++;
return true;
} return false;
} public void Reset() { this.index = -; }
} static void Main(string[] args)
{
MyClass myobject = new MyClass(); foreach (var item in myobject)
{
Console.WriteLine(item);
} }

参考:

http://www.cnblogs.com/artech/archive/2013/04/14/yield-in-wcf-02.html