Josell
Josell

Reputation: 1944

Is there an interface like ICollection<t>, but designed for sorted collections?

...or can I use ICollection with no problem?

I mean, I don't think ICollection was designed for Sorted collections because that could break an application designed for sorted or unserted ICollection objects, but I don't know.

Upvotes: 8

Views: 1824

Answers (2)

lissajous
lissajous

Reputation: 451

This interface can be used to sorted enumerables:

IOrderedEnumerable<TElement>

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

Upvotes: 1

dtb
dtb

Reputation: 217313

I'd say the ICollection<T> Interface is suitable for implementation by sorted collection types, because a sorted collection can be enumerated, added to, removed from, cleared and checked for its contents.

As a counter-example, the IList<T> Interface is probably not suitable, because unlike ICollection<T> it assumes that the collection is a list where the elements can be added at specific positions, which doesn't make sense if the collection itself determines the position of each element.

The sorted collection types in the .NET Framework (the SortedList<TKey, TValue> Class, SortedDictionary<TKey, TValue> Class, and SortedSet<T> Class) all implement ICollection<T> but not IList<T>.

Upvotes: 11

Related Questions