Generics is a type of parameter which allows you to specify an arbitrary type T to a method at compile time having no concrete type specification in the method or class declaration.
Introduction
One of the key concepts introduced in C# 2.0 was generic. A lot of articles have been written on generics but somehow still many developers don’t understand the basic concept of generics. It is very imperative to understand and include generics in the application where it’s required. Keeping this in mind, I decided to write some basic concept pertaining to generic.
Generic can be explained in a very simple way that it is a type of parameter which allows you to specify an arbitrary type T to a method at compile time having no concrete type specification in the method or class declaration.
- public class GenericClass {
- Public void SomeBehaviour < T > (T input) {}
- }
A generic collection is strongly typed (type safe), meaning that you can only put one type of object into it. This will eliminate type mismatches at runtime. The main advantage of generic is code reusability, type-safe as well as performance.
Generics Collection
We have multiple types of a generics collection like a list, Dictionary, Sorted List, Queue. Here we are using List as a generic type collection. It means we can define the list of any type (but one type at a time). For example
- List<int> myIntsList=new List<int> (); // this will create a list of integer at runtime.
- List<string> myStringList=new List<string> (); // this will create a list of string at run time.
- List<object> myObjectList=new List<object> (); // this will create a list of objects at run time.
The non-generic collection like ArrayList, HashTable, SortedList are also used to hold/ persist records but it has some different behavior as compared to the generic collection. The non-generic collection will hold any type of data irrespective of the data type. For example
- ArrayList arr = new ArrayList();
- arr.Add("a");
- arr.Add(4);
- arr.Add(new c1()); arr.Add(new object());
In a normal situation, array list looks good as compared to the list because we can add any type of data in it but it has huge performance issues. Every time when you will fetch record you have no idea of what type of data (int, string, float) will be returned. It may generate an exception at runtime because of wrong casting. To avoid casting errors, we have to cast the data in the appropriate type before it’s used, which is called boxing. This will hugely impact performance. Let’s consider the above arrayList.
- string varstr = arr[0].ToString();
- int varint = Convert.ToInt32(arr[1]);
- c1 objC1 = arr[2] as c1; object obj = arr[3];
Let’s understand generic with another example.
We are creating a method which will reverse the array of any type.

Join the conversation! Your thoughts help the community grow.