对于我的项目,我需要复制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.Current
和IMyEnumerable<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();
}
}