gpt4 book ai didi

c# - 问题复制 C# 枚举器接口(interface)

转载 作者:行者123 更新时间:2023-12-01 22:55:48 25 4
gpt4 key购买 nike

对于我的项目,我需要复制 C# 枚举器接口(interface)层次结构。我继续从 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;
}
}

来源:

关于c# - 问题复制 C# 枚举器接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73285625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com