Generics in C#

This post has already been read 1967 times!

With .NET Framework 2.0 Microsoft added a new feature named Generics .Generics made reusability feature of object orientation stronger by allowing programmers to create various programming elements like classes, interfaces, delegates, functions etc which deals with different type of data without compromising the type safety. It means that programmers can create type safe functions with flexible data types.

So, we will start with the most important question?

Why Generics?

In .NET we have many inbuilt collection classes available in System.Collection namespace. While working with these collection classes we face some performance related issues.

1) These classes works with the ‘object’ data type due to which these classes treat  every value stored inside it as an Object.

2) That when an integer values is stored in it is implicitly converted into object type. This Process is called as Boxing.

3) While retrieving data from the collection, data is retrieved as object type which needs to be type casted to the specific type. This process is called as unboxing.

Every time when you add or remove data from these collection boxing and unboxing is performed which leads to performance degradation and due to use of ‘object’ data type the collections are not type safe.  To overcome these issues we want to have a mechanism which is type safe and eliminates need of boxing and unboxing and it is nothing but ‘Generics’.

Let’s start with an example. Suppose you want to create a Stack class for different data types. There are two ways you may perform this task:

1)      Create different classes to implement stack which works for different data types.

2)      The other way is to create a stack class with ‘object’ as parameter. This approach is better than the first approach but still has limitations.

a)      It degrades the performance because every time while inserting data the values are boxed and while retrieving data need to be unboxed.

So, in the above scenario Generics comes to the rescue to avoid boxing and unboxing mechanism.

How to Implement?

You need to create a class which accepts a data type as parameter in the angular brackets<>.Data types are specified at the time of creating object as illustrated in the code given below.


In the above code,

1) Stack class is created with the Generic type T where T is a placeholder it is replaced with the actual data type when we create object of stack class.

2) Stack class consist of array of type T where the size of array is stored in the variable named ‘size’.

3) Stack class contains three methods Push, Pop and PrintStack.

a) Push – This method is responsible for adding element in stack

b) Pop- This method removes element from stack

c) PrintStack- This is a facilitator method which prints the stack.

Due to use of Generics type T. This stack class is capable to work with different data type.

The following code snippet given  demonstrates the use of stack class with different data types:


In the above code Snippet,

1) objStringStack is an object of Stack class which works with string data type.

2) objIntStack is an  object of Stack class which works with int data type.

This is how we made a collection generic that performs the same functionality for different data types without compromising on type safety and avoids performance degradation as it does box and unbox the values stored in it.

Advantages of Generics

a)      Type safety

b)      Improved performance

c)       Better code reusability  |

Dot net certification | .Net Certification | .Net Certification Course

Leave a Reply
