在C#中声明接口继承

在C#中声明接口继承,第1张

概述参见英文答案 > Why does (does it really?) List<T> implement all these interfaces, not just IList<T>?                                    5个 我对C#中接口继承语法有点不清楚. 例如: public interface IFoo{}public interface I 参见英文答案 > Why does (does it really?) List<T> implement all these interfaces,not just IList<T>?5个
我对C#中接口的继承语法有点不清楚.

例如:

public interface IFoo{}public interface Ibar : IFoo{}

这有什么区别:

public interface IQux : Ibar{}

还有这个:

public interface IQux : Ibar,IFoo{}

或者,对于现实世界的例子,为什么ICollection< T>宣称是这样的:

public interface ICollection<T> : IEnumerable<T>,IEnumerable

而不是这个:

public interface ICollection<T> : IEnumerable<T>

鉴于IEnumerable< T>已经从IEnumerable继承?

解决方法 Eric lippert在本文中解释得非常好:

https://blogs.msdn.microsoft.com/ericlippert/2011/04/04/so-many-interfaces

如果Ibar继承自IFoo,那么从编译器的角度来看,两者之间没有区别:

public interface IQux : Ibar{}

还有这个:

public interface IQux : Ibar,IFoo{}

如果您认为IQux使代码更具可读性,您可以选择声明IQux包含IFoo.或者你可以选择不这样做.

总结

以上是内存溢出为你收集整理的在C#中声明接口继承全部内容,希望文章能够帮你解决在C#中声明接口继承所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1252199.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存