C# Collection
We have learned about an array in the previous section. C# also includes specialized classes that hold many values or objects in a specific series, that are called 'collection'.
There are two types of collections available in C#: non-generic collections and generic collections. We will learn about non-generic collections in this section.
The System.Collections namespace includes the interfaces and classes for the non-generic collections. The following diagram illustrates the hierarchy of the interfaces and classes for the non-generic collections.
As you can see in the above diagram, IEnumerator, IEnumerable, and ICollection are the top level interfaces for all the collections in C#.
IEnumerator: The IEnumerator interface supports a simple iteration over a non-generic collection. It includes methods and property which can be implemented to support easy iteration using foreach loop.
IEnumerable: The IEnumerable interface includes GetEnumerator()
method which returns an object of IEnumerator.
So, all the built-in collection classes and custom collection classes must implement IEnumerator and IEnumerable interfaces for easy iteration using foreach loop.
ICollection: The ICollection interface is the base interface for all the collections that defines sizes, enumerators, and synchronization methods for all non-generic collections. The Queue and Stack collection implement ICollection inferface.
IList: The IList interface includes properties and methods to add, insert, remove elements in the collection and also individual element can be accessed by index. The ArrayList and BitArray collections implement IList interface.
IDictionary: The IDictionary interface represents a non-generic collection of key/value pairs. The Hashtable and SortedList implement IDictionary interface and so they store key/value pairs.
As you can see from the diagram, ArrayList, BitArray, Hashtable, SortedList, Queue, and Stack collections implement different interfaces and so, they are used for the different purposes.
Non-generic Collections | Usage |
---|---|
ArrayList | ArrayList stores objects of any type like an array. However, there is no need to specify the size of the ArrayList like with an array as it grows automatically. |
SortedList | SortedList stores key and value pairs. It automatically arranges elements in ascending order of key by default. C# includes both, generic and non-generic SortedList collection. |
Stack | Stack stores the values in LIFO style (Last In First Out). It provides a Push() method to add a value and Pop() & Peek() methods to retrieve values. C# includes both, generic and non-generic Stack. |
Queue | Queue stores the values in FIFO style (First In First Out). It keeps the order in which the values were added. It provides an Enqueue() method to add values and a Dequeue() method to retrieve values from the collection. C# includes generic and non-generic Queue. |
Hashtable | Hashtable stores key and value pairs. It retrieves the values by comparing the hash value of the keys. |
BitArray | BitArray manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0). |
Let's see each type of collection in the next chapters.