Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当基接口已经继承接口时,为什么再次继承该接口_C#_Oop_Interface - Fatal编程技术网

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
继承。