C# 当基接口已经继承接口时,为什么再次继承该接口
我正在回顾继承,对继承接口有些困惑。让我们以C# 当基接口已经继承接口时,为什么再次继承该接口,c#,oop,interface,C#,Oop,Interface,我正在回顾继承,对继承接口有些困惑。让我们以List为例 我有一个基本声明: List<string> categoryList = new List<string>(); 如果我再次单击,当光标位于IList 公共接口IList:ICollection、IEnumerable、IEnumerable { } 现在我的问题是:List为什么仍然继承ICollection、IEnumerable和IEnumerable,而IList已经继承了它?我相信这正是Visual
List
为例
我有一个基本声明:
List<string> categoryList = new List<string>();
如果我再次单击,当光标位于IList
公共接口IList:ICollection、IEnumerable、IEnumerable
{
}
现在我的问题是:List
为什么仍然继承ICollection
、IEnumerable
和IEnumerable
,而IList
已经继承了它?我相信这正是Visual Studio表示定义的方式,它向您展示了整个接口层次结构
如果查看,您将看到它只实现IList、System.Collections.IList和IReadOnlyList,因为实现IEnumerable是多余的
public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>
{
// bla bla bla
}
公共类列表:IList、System.Collections.IList、IReadOnlyList
{
//呜呜呜呜
}
我相信这正是Visual Studio表示定义的方式,它向您展示了整个界面层次结构
如果查看,您将看到它只实现IList、System.Collections.IList和IReadOnlyList,因为实现IEnumerable是多余的
public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>
{
// bla bla bla
}
公共类列表:IList、System.Collections.IList、IReadOnlyList
{
//呜呜呜呜
}
这显然是为了便于阅读。只要指定三个接口,代码就可以正常编译:
public class List<T> : IList<T>, IList, IReadOnlyList<T>
{
}
公共类列表:IList、IList、IReadOnlyList
{
}
但是通过这种方式,您必须知道,IList
继承了ICollection
和IEnumerable
,并且IList
实现了ICollection
和IEnumerable
,而且,IReadOnlyList
继承了IReadOnlyCollection
这显然是为了可读性。只要指定三个接口,代码就可以正常编译:
public class List<T> : IList<T>, IList, IReadOnlyList<T>
{
}
公共类列表:IList、IList、IReadOnlyList
{
}
但是通过这种方式,您必须知道,IList
继承了ICollection
和IEnumerable
,并且IList
实现了ICollection
和IEnumerable
,而IReadOnlyList
继承了IReadOnlyCollection
+1,因为如果您看到ICollection
接口,它将再次被IEnumarable
继承;如果您看到ICollection
接口,它将再次被IEnumarable
继承。