首页 新闻 会员 周边

List<T>中的Enumerator结构体的问题

0
悬赏园豆:10 [已解决问题] 解决于 2015-04-23 13:14

// 摘要:
// 枚举 System.Collections.Generic.List<T> 的元素。
[Serializable]
public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
{

// 摘要:
// 获取枚举数当前位置的元素。
//
// 返回结果:
// System.Collections.Generic.List<T> 中位于该枚举数当前位置的元素。
public T Current { get; }

// 摘要:
// 释放由 System.Collections.Generic.List<T>.Enumerator 使用的所有资源。
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public void Dispose();
//
// 摘要:
// 使枚举数前进到 System.Collections.Generic.List<T> 的下一个元素。
//
// 返回结果:
// 如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
//
// 异常:
// System.InvalidOperationException:
// 在创建了枚举数后集合被修改了。
public bool MoveNext();
}

它为什么没有实现IEnumerator.Reset()和IEnumerator.Current还是编译通过了,但是自己定义就不能编译。

羁绊|情愫的主页 羁绊|情愫 | 初学一级 | 园豆:193
提问于:2015-04-23 12:20
< >
分享
最佳答案
0

为什么你觉得没实现呢?反遍历List的源代码是可以看到有这个实现的:

/// <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, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get
        {
          return this.current;
        }
      }

      [__DynamicallyInvokable]
      object IEnumerator.Current
      {
        [__DynamicallyInvokable] get
        {
          if (this.index == 0 || this.index == this.list._size + 1)
            ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
          return (object) 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>
      [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
      [__DynamicallyInvokable]
      public void Dispose()
      {
      }

      /// <summary>
      /// Advances the enumerator to the next element of the <see cref="T:System.Collections.Generic.List`1"/>.
      /// </summary>
      /// 
      /// <returns>
      /// true if the enumerator was successfully advanced to the next element; 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 || (uint) this.index >= (uint) list._size)
          return this.MoveNextRare();
        this.current = list._items[this.index];
        ++this.index;
        return true;
      }

      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;
      }

      [__DynamicallyInvokable]
      void IEnumerator.Reset()
      {
        if (this.version != this.list._version)
          ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
        this.index = 0;
        this.current = default (T);
      }
    }
收获园豆:10
幻天芒 | 高人七级 |园豆:37175 | 2015-04-23 13:11

我是才用这个,在vs中看的,以为是没实现,反编译是看出来实现了。谢谢你的回答啊

羁绊|情愫 | 园豆:193 (初学一级) | 2015-04-23 13:13

@羁绊|情愫: :),你看的应该是方法签名。

幻天芒 | 园豆:37175 (高人七级) | 2015-04-23 13:15

@幻天芒: 对了,我还想问一下这些方法它都实现了,它是怎么隐藏的,让它在VS中看不到方法签名的

羁绊|情愫 | 园豆:193 (初学一级) | 2015-04-23 13:21

@羁绊|情愫: 你什么时候在智能提示的列表中见到过 private,protected 修饰的属性、方法?

Launcher | 园豆:45045 (高人七级) | 2015-04-23 13:52

@羁绊|情愫: Launcher大神解释得很清楚。

幻天芒 | 园豆:37175 (高人七级) | 2015-04-23 14:02

@Launcher: 没注意看,是private。=_=|,谢谢你们的解答

羁绊|情愫 | 园豆:193 (初学一级) | 2015-04-23 14:34
其他回答(1)
0

因为它实现了。

Launcher | 园豆:45045 (高人七级) | 2015-04-23 12:55

就是说实现了,但是这里看不到是吧

支持(0) 反对(0) 羁绊|情愫 | 园豆:193 (初学一级) | 2015-04-23 13:05

@羁绊|情愫: 因为访问修饰符是 private,protected,internal(如果它指定了 InternalsVisibleTo 对你的程序集可见,那么此关键字等同于 public)。

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2015-04-23 13:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册