对于我的项目,我需要复制C#枚举器接口层次 struct .我只需从VS F12复制、粘贴和重命名即可:

  public interface IMyEnumerator {
    object Current { get; }
    bool MoveNext();
    void Reset();
  }

  public interface IMyEnumerator<out T> : IMyEnumerator, IDisposable {
    T Current { get; }
  }

  public interface IMyEnumerable {
    IMyEnumerator GetEnumerator();
  }

  public interface IMyEnumerable<out T> : IMyEnumerable {
    IMyEnumerator<T> GetEnumerator();
  }

  public class MyContainer : IMyEnumerable<T>, IMyEnumerable {
    ...
  }
}

我收到了关于IMyEnumerator<out T>.Current隐藏IMyEnumerator.CurrentIMyEnumerable<out T>.GetEnumerator隐藏IMyEnumerable.GetEnumerator的警告,并建议添加new关键字.

在这种情况下,我应该添加关键字new吗?我有点不确定其中的含义,因为IEnumerator和IEnumerable的匹配源(至少从VS F12中可以看出)没有new关键字,请参阅:

namespace System.Collections {
  //
  // Summary:
  //     Supports a simple iteration over a non-generic collection.
  public interface IEnumerator {
    //
    // Summary:
    //     Gets the element in the collection at the current position of the enumerator.
    //
    // Returns:
    //     The element in the collection at the current position of the enumerator.
    object Current { get; }

    //
    // Summary:
    //     Advances the enumerator to the next element of the collection.
    //
    // Returns:
    //     true if the enumerator was successfully advanced to the next element; false if
    //     the enumerator has passed the end of the collection.
    //
    // Exceptions:
    //   T:System.InvalidOperationException:
    //     The collection was modified after the enumerator was created.
    bool MoveNext();
    //
    // Summary:
    //     Sets the enumerator to its initial position, which is before the first element
    //     in the collection.
    //
    // Exceptions:
    //   T:System.InvalidOperationException:
    //     The collection was modified after the enumerator was created.
    void Reset();
  }
  //
  // Summary:
  //     Exposes an enumerator, which supports a simple iteration over a non-generic collection.
  public interface IEnumerable {
    //
    // Summary:
    //     Returns an enumerator that iterates through a collection.
    //
    // Returns:
    //     An System.Collections.IEnumerator object that can be used to iterate through
    //     the collection.
    IEnumerator GetEnumerator();
  }
}

namespace System.Collections.Generic {
  //
  // Summary:
  //     Supports a simple iteration over a generic collection.
  //
  // Type parameters:
  //   T:
  //     The type of objects to enumerate.
  public interface IEnumerator<out T> : IEnumerator, IDisposable {
    //
    // Summary:
    //     Gets the element in the collection at the current position of the enumerator.
    //
    // Returns:
    //     The element in the collection at the current position of the enumerator.
    T Current { get; }
  }

  //
  // Summary:
  //     Exposes the enumerator, which supports a simple iteration over a collection of
  //     a specified type.
  //
  // Type parameters:
  //   T:
  //     The type of objects to enumerate.
  public interface IEnumerable<out T> : IEnumerable {
    //
    // Summary:
    //     Returns an enumerator that iterates through the collection.
    //
    // Returns:
    //     An enumerator that can be used to iterate through the collection.
    IEnumerator<T> GetEnumerator();
  }
}

推荐答案

如果你参考源代码,你会发现IEnumerator<T>实际上使用了new关键字:

public interface IEnumerator<out T> : IDisposable, IEnumerator
{
    // Returns the current element of the enumeration. The returned value is
    // undefined before the first call to MoveNext and following a
    // call to MoveNext that returned false. Multiple calls to
    // GetCurrent with no intervening calls to MoveNext
    // will return the same object.
    new T Current
    {
        get;
    }
}

资料来源:

Csharp相关问答推荐

如何注册实现同一接口的多个服务并注入到控制器构造函数中

当通过Google的Gmail Api发送邮件时,签名会产生dkim = neutral(正文散列未验证)'

AsNoTrackingWithIdentitySolutions()似乎不起作用?

使页面内容居中

不带身份的Blazor服务器.Net 8 Cookie身份验证

无法通过绑定禁用条目

如何从ASP.NET核心MVC视图和Blazor传递数据

在C#中反序列化/序列化具有混合元素顺序的XML时出现问题

什么类型的对象存储在大对象堆(LOH)中

在ASP.NET Core 8 MVC中本地化共享视图

获取混淆&Quot;模糊引用&Quot;错误

在平行内使用跨度.用于循环

try 创建一个C#程序,该程序使用自动实现的属性、覆盖ToString()并使用子类

如何从SignalR获取连接客户端的域

删除MudRadio时,MudRadioGroup未 Select 正确的MudRadio

Xamarin.Forms中具有类似AspectFill的图像zoom 的水平滚动视图

如果使用LINQ的值为空,则C#不使用GroupBy进行聚合

引用类型中的值类型属性是否包含装箱的值?

HttpClient.PostAsJsonAsync在Blazor中不起作用

更改选项卡页面图标 colored颜色