加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#中声明接口继承

发布时间:2020-12-15 08:47:07 所属栏目:百科 来源:网络整理
导读:参见英文答案 Why does (does it really?) ListT implement all these interfaces,not just IListT?5个 我对C#中接口的继承语法有点不清楚. 例如: public interface IFoo{}public interface IBar : IFoo{} 这有什么区别: public interface IQux : IBar{}
参见英文答案 > 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.或者你可以选择不这样做.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读