Collections In C#


What is a Collection? Before we discuss this, let me tell you that Collection comes under Generic and Non- Generic classes. Thus, let's first discuss Generic Class.

Generic classes were introduced in C# 2.0. Through these classes, we can create the Collections, which are used to contain the data. Generic classes are used to provide the type safe structure --  type safe means only one type of data can be contained within Generic classes. The data type is defined at the time of an object initialization. We can have a different type of collection. Some of them come under Generic classes and others come under Non- Generic classes. Generic collection provides some benefits over Non -Generic class. Let's see which class comes under Generic and Non-Generic classes.

Generic Classes or Generic Collection

  1. List<T>
    In C#, we can use the list class to contain the data and this class provides many methods like add, count etc. This class comes under Generic Collection. The reason behind this is, it provides the type safe structure for which Generic feature was introduced. Type safe means we will declare the data type of the data, which list will hold it at the time of its initialization. Thus, it will contain only one type of data. There is no need of any conversion to store it in the memory, because we already mentioned what data the  list will hold. Let's see the example, given below, to make it more clear.

    1. List<Int32> objlist = new List<int>();   
    2. objlist.Add(1);   
    3. objlist.Add(2);   
    4. objlist.Add(3);   
    You can see in the example, given above, that we define the type int in the list at the time of its initialization. Thus, the list will hold only integer type of data and there will not be any typecasting. Typecasting means the conversation of the datatype to an object or an object to the datatype. Thus, you must be familiar now about Safe Type feature in Generics. If you try to pass the string or any other data type value in this list in the example, mentioned above, it will throw an error regarding the invalid value. Check the image, given below:



    Thus, you can see the error of the invalid arguments because we are using Generic Collection. Define the type int and pass the string data in it. Type safe means passing the data, according to the datatype or an object, we used at the time of initialization. You can use Int, String, Float or any Class you defined, while using list.

  2. Dictionary<Tkey,TValue>
    Another collection that comes under Generic is Dictionary. Dictionary and list both come under Generic, but the difference between both of them is that dictionary contains the data in key and value pair, whereas the list contains only data and not the key; otherwise, there is no difference between both of them. Let's see an example of dictionary.

    1. Dictionary<int, String> objDictionary = new Dictionary<intstring>();   
    2. objDictionary.Add(1, "A");   
    3. objDictionary.Add(2, "B");   
    4. objDictionary.Add(3, "C");  

Thus, you can see in the example, mentioned above, how we can use dictionary to contain the data and this comes under Generic because it also asked to define the datatype at the time of its initialization.

It's not necessary to define the key as an integer. You can define it as a string or any other data type, but you have to define the type and have to pass the data according to it. The disadvantage of it is it will throw an error if you try to find a value of the key, which doesn't exist in the collection.

Now let's see the Non-Generic Classes

  1. Arraylist
    Arraylist belongs to Non-Generic Classes. It is also a collection, but doesn't support Type Safety, as it means we can increase Arraylist and can put any kind of data in it. Arraylist can contain any data type of data. The problem is, we don't declare the data type at the time of an arraylist initialization, which means it can contain any datatype data, so what happens internally is, everytime the data of an arraylist is saved in the memory as an object and to save this data as an object in the memory, a conversion process was working on the data. The reason for this conversion is that, we don't know what kind of data arraylist contains, so that data can't be saved directly as its data type, because it can have different data type data. Typecasting takes place in Non -Generic classes. We don't prefer to use Non Generic classes over Generic classes, because it's heavy and takes time in the conversion.
    1. ArrayList objarrl = new ArrayList();   
    2. objarrl.Add(1);   
    3. objarrl.Add("Two");   
    4. objarrl.Add(3.0);   
    You can see in the example, mentioned above, that while creating an object of an arraylist, we don't define the type of data it will hold, because it doesn't provide type safety. It can contain any type of data.

  2. Hashtable
    Hashtable comes under Non Generic classes. It is similar to dictionary in the way of saving or containing the data. It also saves the data in the key value pair. Now, the question comes into mind. What is the difference between dictionary and hashtable? The main difference is both belong to different classes and Hashtable doesn't care about the datatype, it can contain key and value of any data type, because it doesn't provide type safety but on the other hand dictionary provides type safety and it can contain data of data type , which is declared at the time of its initialization only. Hashtable has one benefit over dictionary, i.e. it doesn't return or invoke any error, if you try to get the value of a key which doesn't exist  it will return null value in that case.

    1. Hashtable hashtable = new Hashtable();   
    2. hashtable.Add(1, 1);   
    3. hashtable.Add("b", 2);   
    4. hashtable.Add("c", 3);   

I hope, somehow, this article made somethings clear to you about Generic and Non Generic Collections. When you go for an interview next time and any question is asked from you regarding the Collection, you will able to give a clear explanation of it.

Up Next
    Ebook Download
    View all
    View all