...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.
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.
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> ).
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.
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>.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With