【问题标题】:Is there an interface like ICollection<t>, but designed for sorted collections?是否有像 ICollection<t> 这样的接口,但设计用于排序集合?
【发布时间】:2012-10-29 23:31:43
【问题描述】:

...或者我可以毫无问题地使用 ICollection 吗?

我的意思是,我不认为 ICollection 是为排序集合设计的,因为这可能会破坏为排序或未排序的 ICollection 对象设计的应用程序,但我不知道。

【问题讨论】:

  • 查看SortedDictionary的定义:public class SortedDictionary&lt;TKey, TValue&gt; : IDictionary&lt;TKey, TValue&gt;, ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;, IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;, IDictionary, ICollection, IEnumerable {}
  • SortedSet 和SortedList 都继承自ICollection 以及其他一些接口。

标签: c# .net interface


【解决方案1】:

该接口可用于对枚举进行排序:

IOrderedEnumerable&lt;TElement&gt;

https://docs.microsoft.com/en-us/dotnet/api/system.linq.iorderedenumerable-1?view=netcore-3.1

【讨论】:

    【解决方案2】:

    我想说ICollection<T> Interface 适合通过排序集合类型实现,因为排序集合可以枚举、添加、删除、清除和检查其内容。

    作为一个反例,IList<T> Interface 可能不合适,因为与 ICollection 不同,它假定集合是一个列表,其中元素可以添加到特定位置,如果集合本身决定了每个元素的位置。

    .NET Framework 中的排序集合类型(SortedList<TKey, TValue> Class、SortedDictionary<TKey, TValue> Class 和 SortedSet<T> Class)都实现了 ICollection,但没有实现 IList。

    【讨论】:

      猜你喜欢
      • 2018-01-16
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      相关资源
      最近更新 更多