Csharp Collection
# C# Collection
In C#, **Collection classes** are a category of classes **specifically designed for data storage and retrieval**.
Collections allow us to easily store, manage, and manipulate a group of data, such as:
* Accessing items in a list by index;
* Accessing values in a dictionary by key;
* Dynamically adding or deleting elements, etc.
Collection classes provide support for various common data structures, for example:
* **Stack** -- Last-In-First-Out (LIFO) structure;
* **Queue** -- First-In-First-Out (FIFO) structure;
* **List** -- A dynamically expandable ordered sequence;
* **HashTable** -- Fast data lookup via key-value pairs.
Furthermore, most collection classes implement the **same interfaces**,
which means they have similar operational methods, such as adding, deleting, and traversing.
In earlier versions of C#, elements in a collection were typically stored as `Object` type, allowing us to put **any type of object** into the collection. This is because in C#, `Object` is the base class of all data types -- whether it's `int`, `string`, or a custom class, they all ultimately inherit from `Object`.
## Various Collection Classes and Their Usage
Below are various commonly used classes from the **System.Collection** namespace. Click the links below for details.
| Class | Description and Usage |
| --- | --- |
| (#) | It represents an ordered collection of objects that can be individually **indexed**. It can essentially replace an array. However, unlike an array, you can use an **index** to add and remove items at specified positions, and the ArrayList automatically resizes itself. It also allows for dynamic memory allocation, addition, searching, and sorting of items within the list. |
| (#) | It uses **keys** to access elements in the collection. When you need to access elements using a key, you use a Hashtable, and you can identify a useful key value. Each item in a Hashtable has a **key/value** pair. The key is used to access items in the collection. |
| (#) | It can use both **keys** and **indices** to access items in the list. A SortedList is a combination of an array and a Hashtable. It contains a list of items that can be accessed using either keys or indices. If you use an index to access items, it acts like an ArrayList; if you use a key to access items, it acts like a Hashtable. Items in the collection are always sorted by key value. |
| (#) | It represents a collection of objects based on the **Last-In-First-Out (LIFO)** principle. You use a Stack when you need LIFO access to items. When you add an item to the list, it's called **pushing** an element; when you remove an item from the list, it's called **popping** an element. |
| (#) | It represents a collection of objects based on the **First-In-First-Out (FIFO)** principle. You use a Queue when you need FIFO access to items. When you add an item to the list, it's called **enqueueing**; when you remove an item from the list, it's called **dequeueing**. |
| (#) | It represents a **binary** array using the values 1 and 0. You use a BitArray when you need to store bits but don't know the number of bits in advance. You can access items in the BitArray collection using an **integer index**, starting from zero. |
YouTip