2017-04-10 58 views
1

在.net中,这是如何定义IEnumerableIEnumerable<T>。如果你定义了这样的东西(例如IMyInterface和IMyInterface),你会得到一个警告,在你的方法前面添加new,这怎么可能?IEnumerable.GetEnumerator()和IEnumerable <T> .GetEnumerator()为什么它们没有新的关键字

(我知道实现我已经明确地实现它,我只是问definign接口,即使没有实现)

public interface IEnumerable 
    { 
     IEnumerator GetEnumerator(); 
    } 

public interface IEnumerable<out T> : IEnumerable 
    { 
     IEnumerator<T> GetEnumerator(); 
    } 

public interface IMy 
    { 
     IQueryable GetAll(); 
    } 

public interface IMy<T> : IMy 
    { 
     IQueryable<T> GetAll(); 
    } 
+3

是什么让你觉得它不是[用新声明](https://referencesource.microsoft.com/#mscorlib/system/collections/generic/ienumerable.cs,35)? –

+0

您无法声明新的IEnumerable或新的IMy。你必须定义一个使用该接口的类并声明新的类。 –

+0

值得注意的是,在这种特殊情况下,“新”关键字从编译的角度来说什么也不做。这只是为了摆脱编译器警告。换句话说,编译器在说“要小心,你隐藏了一个成员”,而你正在告诉编译器“我知道我在隐藏它,不要警告我”。 – JuanR

回答

0

由于Damiencommented已经,它有关联的new关键字。

Reference Source

public interface IEnumerable<out T> : IEnumerable 
{ 
    // Returns an IEnumerator for this enumerable Object. The enumerator provides 
    // a simple way to access all the contents of a collection. 
    /// <include file='doc\IEnumerable.uex' path='docs/doc[@for="IEnumerable.GetEnumerator"]/*' /> 
    new IEnumerator<T> GetEnumerator(); 
} 

如果让Visual Studio中创建一个存根,它的确会不会有new关键字。那取决于你。

+0

这个问题不应该被当作“不能再生产的问题”来解决吗? – DavidG

+0

这对我也很好。我刚刚看到2个删除的答案,我认为它需要一个。如果OP想要,他甚至可以删除它。 –

+0

@DavidG我保留这个问题以防其他人在将来有同样的问题 – RezaRahmati

相关问题