【Unity|C#】基础篇(20)——枚举器与迭代器(IEnumerable/IEnumerator)

【学习资料】

  《C#图解教程》(第18章)https://www.cnblogs.com/moonache/p/7687551.html
  电子书下载:https://pan.baidu.com/s/1mhOmBG0

 


 【笔记】

    传送门(看这篇就好了) https://www.cnblogs.com/moonache/p/6548043.html

 

  •  List 枚举器实现源码
    • 获取枚举器                  :GetEnumerator()
    • 当前迭代对象(只读)   :Current
    • 继续往下迭代               :MoveNext()
    • 重置迭代                     :Reset()
  • namespace System.Collections.Generic
    {
        /// Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.To browse the .NET Framework source code for this type, see the Reference Source.
        /// The type of elements in the list.
        [__DynamicallyInvokable, DebuggerDisplay("Count = {Count}"), DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
        [Serializable]
        public class List : IList, ICollection, IEnumerable, IEnumerable, IList, ICollection, IReadOnlyList, IReadOnlyCollection
        {
            // ......
            // ......
            // ......
            
            /// Returns an enumerator that iterates through the .
            /// A  for the .
            [__DynamicallyInvokable]
            public List.Enumerator GetEnumerator()
            {
                return new List.Enumerator(this);
            }
    
            [__DynamicallyInvokable]
            IEnumerator IEnumerable.GetEnumerator()
            {
                return new List.Enumerator(this);
            }
    
            /// Returns an enumerator that iterates through a collection.
            /// An  that can be used to iterate through the collection.
            [__DynamicallyInvokable]
            IEnumerator IEnumerable.GetEnumerator()
            {
                return new List.Enumerator(this);
            }
    
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // 枚举器实现源码
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            /// Enumerates the elements of a .
            [__DynamicallyInvokable]
            [Serializable]
            public struct Enumerator : IEnumerator, IDisposable, IEnumerator
            {
                private List list;
    
                private int index;
    
                private int version;
    
                private T current;
    
                /// Gets the element at the current position of the enumerator.
                /// The element in the  at the current position of the enumerator.
                [__DynamicallyInvokable]
                public T Current
                {
                    [__DynamicallyInvokable]
                    get
                    {
                        return this.current;
                    }
                }
    
                /// Gets the element at the current position of the enumerator.
                /// The element in the  at the current position of the enumerator.
                /// The enumerator is positioned before the first element of the collection or after the last element. 
                [__DynamicallyInvokable]
                object IEnumerator.Current
                {
                    [__DynamicallyInvokable]
                    get
                    {
                        if (this.index == 0 || this.index == this.list._size + 1)
                        {
                            ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
                        }
                        return this.Current;
                    }
                }
    
                internal Enumerator(List list)
                {
                    this.list = list;
                    this.index = 0;
                    this.version = list._version;
                    this.current = default(T);
                }
    
                /// Releases all resources used by the .
                [__DynamicallyInvokable]
                public void Dispose()
                {
                }
    
                /// Advances the enumerator to the next element of the .
                /// 
                ///      if the enumerator was successfully advanced to the next element;  if the enumerator has passed the end of the collection.
                /// The collection was modified after the enumerator was created. 
                [__DynamicallyInvokable]
                public bool MoveNext()
                {
                    List list = this.list;
                    if (this.version == list._version && this.index < list._size)
                    {
                        this.current = list._items[this.index];
                        this.index++;
                        return true;
                    }
                    return this.MoveNextRare();
                }
    
                private bool MoveNextRare()
                {
                    if (this.version != this.list._version)
                    {
                        ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
                    }
                    this.index = this.list._size + 1;
                    this.current = default(T);
                    return false;
                }
    
                /// Sets the enumerator to its initial position, which is before the first element in the collection.
                /// The collection was modified after the enumerator was created. 
                [__DynamicallyInvokable]
                void IEnumerator.Reset()
                {
                    if (this.version != this.list._version)
                    {
                        ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
                    }
                    this.index = 0;
                    this.current = default(T);
                }
            }
        }
    }

 

你可能感兴趣的:(【Unity|C#】基础篇(20)——枚举器与迭代器(IEnumerable/IEnumerator))