C# collection
The Collection class is a class dedicated to data storage and retrieval. These classes provide support for stack, queue, list, and hash table. Most collection classes implement the same interface.
The Collection class serves different purposes, such as dynamically allocating memory for elements, accessing list items based on indexes, and so on. These classes create a collection of objects of the Object class. In C#, the Object class is the base class for all data types.
Various collection classes and their usage
Here are a variety of commonly used System.Collection
class of the namespace. Click the link below to see the details.
Class |
Description and usage |
---|---|
ArrayList |
It represents an ordered collection of objects that can be indexed separately. It can basically replace an array. However, unlike an array, youcan use the index to add and remove items at a specified location, and the dynamic array automatically resizes it. It also allows dynamic memory allocation, adding, searching, and sorting items in the list. |
Hash table |
It uses keys to access elements in the collection. When you use keys to access elements, a hash table is used, and you can identify a useful key value. Each item in the hash table has a key/value pair. Key is used to access items in the collection. |
SortedList |
It can use keys and indexes to access items in the list. A sorted list is a combination of arrays and hash tables. It contains a list of items that can be accessed using keys or indexes. If you use an index to access items, it is a dynamic arrayList, and if you use keys to access items, it is aHashtable. Items in the collection are always sorted by key value. |
Stack |
It represents a collection of last-in, first-out objects. Use the stack whenyou need last-in-first-out access to items. When you add an item to the list called a push element, when you remove an item from the list, it is called a pop-up element. |
Queue |
It represents a collection of first-in, first-out objects. Queues are used when you need first-in, first-out access to items. When you add an item to the list, it is called queuing, and when you remove an item from the list, it is called queuing. |
BitArray |
It represents a binary array represented by values 1 and 0. Use a point array when you need to store bits but do not know the number of bits in advance. You can access items from the dot array collection using an integerindex, starting from scratch. |