Collections helps in storage of data in an orderly way. The data stored in a Collection are called as items or elements. Collections are preferred when the number of items to be stored is not predetermined. Hence, collections are dynamic in nature and their size can increase or decrease during the execution of a program.
There are various type of collections classes and these can be found in the below namespaces:-
- System.Collections
- System.Collections.Generic
- System.Collections.Specialized
- System.Collections.Concurrent
- System.Collections.ObjectModel
Types of Collections in C#
The collection classes most often used in C# are listed below:
Collection Class | Description |
---|---|
Dictionary | All the elements of a Dictionary class is a collection of Key/Value pairs. It's a generic class and the syntax of the class is Dictionary<TKey,TValue> |
List | List classes are also generic in nature and contains a collection of objects. These objects can be of value type or reference type. |
Arraylist | Arraylist is similar to List classes but are less efficient compared to List classes. Also, they are non-generic in nature and all elements are stored as object types in this collection. |
Queue | Queue is a special type of collection in which items that are inserted first are removed first. They follow the First-In-First-Out pattern. Also known as FIFO pattern. |
Stack | Stack is also a special type of collection in which the elements which are inserted first are removed last. They follow the Last-In-First-Out pattern. Also known as LIFO pattern. |
Hashtable | Hashtable is a standard Dictionary object in which all the elements have a key and a corresponding value. |