How to create a generic class in C#

Creating a generic class in C# is very simple. You just need to learn a very simple formula and you will be able to write your own generic classes. In this tutorial we will take a very simple example to explain how to write generic classes.

Let’s write a Stack class which stores integer variables

Here we are writing a very simple stack class which stores integer variables. The stack class we are writing will be very simple and will just store one value at a time. But from this class you will easily learn how to write generic classes. Look at the code below

class Stack  {        Int data;      public void Push(int input)      {          data = input;      }        public int Pop()      {          return data;      }  }

As you can see in this code the Push and the Pop functions accept and return integer type values respectively. The variable which stores this value is also of the type int. To use this stack you will create the object of this stack class and Push data to it like this

Stack stack = new Stack();  Stack.Push(10);  int x = stack.Pop();

Till this point everything is OK. Now let’s add a twist here. Suppose you want to use this Stack class to store float type values, then what will you do? Do you think it is possible for you to use this Stack class to store float type values? The answer is no. Why? Because we have fixed the type of values it can take. In this case this Stack class can only work with integer type values.

Creating a generic class

Now let’s use the magic of generics and change the code of our Stack class so that we can use this Stack to work with any type of values and not just integer values. Look at the modified Stack class below

class Stack<T>  {      T data;            public void Push(T input)      {          data = input;      }        public T Pop()      {          return data;      }  }

You notice the changes we have made? Here is what we have done

  1. Changed the line class Stack to class Stack<T>.  When you add < > in front of the class it becomes a generic class. You can write any variable name inside < >, like we have written <T> in this case. What is the meaning of T? Nothing as such, it is just acting as a placeholder. During the object creation we will replace T with the type of data we want the stack to store for us. It is not compulsory to use the name T in generics, you can use any name you like. The only condition is that this name should not be pre defined in C# and also should not have a space in its name.
  2. We have replace int data type with T everywhere in the Stack class. By doing this we get the power of replacing T with any data type during the object creation.

How to create the object of this generic class

Look at the code below and see how we are replacing T with the data type of our choice during the object creation.

Stack<int> stack = new Stack<int>();

In the above code we are asking the compiler to replace T with int everywhere inside the Stack class.

Stack<float> stack = new Stack<float>();

In the above code we are asking the compiler to replace T with float everywhere inside the Stack class.

So now you can say that your Stack class has become a generic class, you can use the same functionality with any data type you want by just replace T with that data type.

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp