Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use Collection<T> vs List<T> [duplicate]

People also ask

When would you use collection over list?

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.

What is the difference between collection and list?

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.

Which is better IList or list?

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.

What is the difference between list and collection in C#?

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