【发布时间】:2020-09-09 11:21:18
【问题描述】:
我想问一下如何正确地从排序列表中继承一个类:
public class Plates : SortedList<string,Plate> {
}
上面的代码没有给我错误,我很困惑,因为为了继承 Enumerable 我需要添加额外的方法来枚举元素。
谁能告诉我如何正确添加排序列表的索引和添加方法?
我是否应该编写类似于下面的代码来将 Plate 对象存储在此类中。除了 IEnumerable 之外,我真的没有太多继承其他类的经验。
public class Plates : SortedList<string,Plate> {
SortedList<string,Plate> plateList = new SortedList<string,Plate>();
}
【问题讨论】:
-
我认为更大的问题是:为什么要“从排序列表中继承一个类”?
-
IEnumerable 是一个接口,SortedList 是一个类。这就是为什么在第二种情况下您不需要额外的方法。
-
IEnumerable是一个接口。它不是继承的,而是实施的。尽管有许多扩展方法在IEnumerable“免费”上运行,但它没有自己的默认实现。 -
那么
to add additional methods to enumerate elements有什么问题? - 你可以枚举this例如:dotnetfiddle.net/Tz4LFI -
because for inheriting Enumerable你能给我们指出这个Enumerable类,以便我们看看它的作用吗?
标签: c# sortedlist