Generic Collections in C#

Generic Collections in C#

In this article, I am going to give a brief introduction to Generic Collections in C#. Please read our previous article where we discussed the Advantages and Disadvantages of the Non-Generic Collection in C#. As part of this article, first, we will discuss the problems of non-generic collections and then we will discuss how to overcome the non-generic collection problems with generic collections in C#. Finally, we will discuss some of the generic collection classes examples in C#. 

The Generic Collections in C# are introduced as part of C# 2.0. You can consider this Generic collection as an extension to the non-generic collection classes which we have already discussed in our previous articles such as ArrayList, Hashtable, Stack, and Queue.

Problems with Non-Generic Collections in C#

The non-generic collection classes such as ArrayList, Hashtable, Stack, and Queue are worked on the object data type. That means the elements are added to the collection are of an object type. As these non-generic collection classes worked on object data type, we can store any type of values that may lead to a runtime exception due to type mismatch. But with Generic Collections in C#, now are able to store a specific type of data (whether a primitive type or a reference type) which provides type safety by eliminating the type mismatch at run time.

The second problem with non-generic collection classes is that we get performance overhead. The reason for this is boxing and unboxing. As we already discussed these collection classes are worked on the object data type. So if we are storing value type data into the collection, then those value type data are first converted into object type and then only store into the collection which is nothing but performing boxing. Similarly, if we want to retrieve the data from the collection, then we need to convert the data from object type to value type means performing unboxing. Due to this boxing and unboxing, we get poor performance if our collection is a big one.

Note: Boxing means converting a value type to an object type and Unboxing means converting object type back to the value type.

The solution to the Non-Generic Collection Problem

The above two problems of non-generic collections are overcome by the Generic collections in C#. The dot net framework has re-implemented all the existing collection classes such as ArrayList, Hashtable, Stack, and Queue, etc. in Generic Collections such as ArrayList<T>, Dictionary<TKey, TValue>, Stack<T> and Queue<T>. Here T is nothing but the type of values that we want to store in the collection. So. the most important point that you need to remember is while creating the objects of Generic Collection classes, you need to explicitly provide the type of values that you are going to store into the collection.

The Generic Collection classes are implemented under the System.Collections.Generic namespace. The classes which are present in this namespace are as follows.

  1. Stack<T>,
  2. Queue<T>,
  3. LinkedList<T>,
  4. SortedList<T>,
  5. List<T>,
  6. Dictionary<TKey, Tvalue>

Note: Here the <T> refers to the type of values we want to store under them.


Generic Collections in C#

It is also possible to store a user-defined type like a class type or structure type as shown below
List<Customer> listCustomer = new List<Customer> ();
Assume the Customer is a user-defined class type that represents an entity Customer, Here we can store the customer objects within the listCustomer collection where each customer object can internally represent different attributes of customer like id, name, balance, city, state, etc.

Generic Collections in C#:

The Generic Collections in C# are strongly typed. The strongly typed nature allows these collection classes to store only one type of value into it. This not only eliminates the type mismatch at runtime but also we will get better performance as they don’t require boxing and unboxing while they store value type data.

So, It is always a preferable and a good programming choice to use the Generics Collection Classes in C# rather than using the Non-Generic Collection Classes.

In the next article, I am going to discuss how to implement Generics in C# with examples. Here, in this article, I gave a brief introduction to Generic Collections in C#. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.