Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

interface

...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.

like image 223
Josell Avatar asked Oct 29 '12 23:10

Josell


People also ask

What is the difference between ICollection and IList?

An IList extends ICollection. An IList can perform all operations combined from IEnumerable and ICollection, and some more operations like inserting or removing an element in the middle of a list.

Should I use IList or ICollection?

IList<T> is essentially an ICollection<T> with random order-based access. In this case you should decide whether or not your results require list semantics such as order based indexing (then use IList<T> ) or whether you just need to return an unordered "bag" of results (then use ICollection<T> ).

How does SortedSet work c#?

In SortedSet, the elements must be unique. In SortedSet, the order of the element is ascending. It is generally used when we want to use SortedSet class if you have to store unique elements and maintain ascending order. In SortedSet, the user can only store the same type of elements.


2 Answers

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>.

like image 162
dtb Avatar answered Oct 05 '22 23:10

dtb


This interface can be used to sorted enumerables:

IOrderedEnumerable<TElement>

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

like image 43
lissajous Avatar answered Oct 06 '22 01:10

lissajous