为什么不使用yield而不是List <t> .Enumerator

时间:2016-12-26 03:32:36

标签: c# yield

我发现List&lt;&gt;中有一个名为Enumerator的嵌套结构。类。

// Returns an enumerator for this list with the given
// permission for removal of elements. If modifications made to the list 
// while an enumeration is in progress, the MoveNext and 
// GetObject methods of the enumerator will throw an exception.
//
public Enumerator GetEnumerator() {
    return new Enumerator(this);
}

[Serializable]
public struct Enumerator : IEnumerator<T>, System.Collections.IEnumerator
{
    private List<T> list;
    private int index;
    private int version;
    private T current;

    internal Enumerator(List<T> list) {
        this.list = list;
        index = 0;
        version = list._version;
        current = default(T);
    }

    //Omit other code
}

据我所知,yield关键字将转换为IL级别的嵌套结构,为什么Microsoft不直接使用yield?

0 个答案:

没有答案