【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
    {
        /// <summary>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.</summary>
        /// <typeparam name="T">The type of elements in the list.</typeparam>
        [__DynamicallyInvokable, DebuggerDisplay("Count = {Count}"), DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
        [Serializable]
        public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>
        {
            // ......
            // ......
            // ......
            
            /// <summary>Returns an enumerator that iterates through the <see cref="T:System.Collections.Generic.List`1" />.</summary>
            /// <returns>A <see cref="T:System.Collections.Generic.List`1.Enumerator" /> for the <see cref="T:System.Collections.Generic.List`1" />.</returns>
            [__DynamicallyInvokable]
            public List<T>.Enumerator GetEnumerator()
            {
                return new List<T>.Enumerator(this);
            }
    
            [__DynamicallyInvokable]
            IEnumerator<T> IEnumerable<T>.GetEnumerator()
            {
                return new List<T>.Enumerator(this);
            }
    
            /// <summary>Returns an enumerator that iterates through a collection.</summary>
            /// <returns>An <see cref="T:System.Collections.IEnumerator" /> that can be used to iterate through the collection.</returns>
            [__DynamicallyInvokable]
            IEnumerator IEnumerable.GetEnumerator()
            {
                return new List<T>.Enumerator(this);
            }
    
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // 枚举器实现源码
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            /// <summary>Enumerates the elements of a <see cref="T:System.Collections.Generic.List`1" />.</summary>
            [__DynamicallyInvokable]
            [Serializable]
            public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
            {
                private List<T> list;
    
                private int index;
    
                private int version;
    
                private T current;
    
                /// <summary>Gets the element at the current position of the enumerator.</summary>
                /// <returns>The element in the <see cref="T:System.Collections.Generic.List`1" /> at the current position of the enumerator.</returns>
                [__DynamicallyInvokable]
                public T Current
                {
                    [__DynamicallyInvokable]
                    get
                    {
                        return this.current;
                    }
                }
    
                /// <summary>Gets the element at the current position of the enumerator.</summary>
                /// <returns>The element in the <see cref="T:System.Collections.Generic.List`1" /> at the current position of the enumerator.</returns>
                /// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element. </exception>
                [__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<T> list)
                {
                    this.list = list;
                    this.index = 0;
                    this.version = list._version;
                    this.current = default(T);
                }
    
                /// <summary>Releases all resources used by the <see cref="T:System.Collections.Generic.List`1.Enumerator" />.</summary>
                [__DynamicallyInvokable]
                public void Dispose()
                {
                }
    
                /// <summary>Advances the enumerator to the next element of the <see cref="T:System.Collections.Generic.List`1" />.</summary>
                /// <returns>
                ///     <see langword="true" /> if the enumerator was successfully advanced to the next element; <see langword="false" /> if the enumerator has passed the end of the collection.</returns>
                /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
                [__DynamicallyInvokable]
                public bool MoveNext()
                {
                    List<T> 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;
                }
    
                /// <summary>Sets the enumerator to its initial position, which is before the first element in the collection.</summary>
                /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
                [__DynamicallyInvokable]
                void IEnumerator.Reset()
                {
                    if (this.version != this.list._version)
                    {
                        ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
                    }
                    this.index = 0;
                    this.current = default(T);
                }
            }
        }
    }

猜你喜欢

转载自www.cnblogs.com/shahdza/p/12285000.html