If the data is sequential such that the order of the elements matter and you are allowing duplicates, then use a list. If order of elements does not matter and duplicates may or may not be allowed, then use a collection.
In List, data is in particular order. In Set, it can not contain the same data twice. In Collection, it just stores data with no particular order and can contain duplicate data.
The main difference between List and IList in C# is that List is a class that represents a list of objects which can be accessed by index while IList is an interface that represents a collection of objects which can be accessed by index.
Collection is a modifiable set. You can add and remove objects from the set, you can also get the count of items in the set. But there still is no order, and because there is no order: no way to access an item by index, nor is there any way to sort. List is an ordered set of objects.
Collection<T>
:
Provides the base class for a generic collection.
List<T>
:
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
So, according the docs, one is intended as a base class for collections. The other is intended for use as a container.
So use List<T>
and inherit from Collection<T>
.
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