在C#中声明接口继承

Mr *_*son 6 .net c#

我对C#中接口的继承语法有点不清楚.

例如:

public interface IFoo
{
}
public interface IBar : IFoo
{
}
Run Code Online (Sandbox Code Playgroud)

这有什么区别:

public interface IQux : IBar
{
}
Run Code Online (Sandbox Code Playgroud)

还有这个:

public interface IQux : IBar, IFoo
{
}
Run Code Online (Sandbox Code Playgroud)

或者,对于一个真实世界的例子,为什么ICollection<T>声明如下:

public interface ICollection<T> : IEnumerable<T>, IEnumerable
Run Code Online (Sandbox Code Playgroud)

而不是这个:

public interface ICollection<T> : IEnumerable<T>
Run Code Online (Sandbox Code Playgroud)

鉴于IEnumerable<T>已经继承了IEnumerable

And*_*erd 5

Eric Lippert在本文中解释得非常好:

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

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

public interface IQux : IBar
{
}
Run Code Online (Sandbox Code Playgroud)

还有这个:

public interface IQux : IBar, IFoo
{
}
Run Code Online (Sandbox Code Playgroud)

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