Introduction
I would like to share the basics of generics. Here first we will learn the problem statement and then resolve the problem using Generics.
Problem statement
First create a class as in the following code.
- class CompareClass {
- public bool Compare(string x, string y) {
- if (x.Equals(y)) return true;
- else return false;
- }
- public bool Compare(int x, int y) {
- if (x.Equals(y)) return true;
- else return false;
- }
- }
Understanding the code
We created the CompareClass.
Here we created two compare methods, one for the string data type and the second for an int data type.
So the class contains overloaded compare functions.
Problem
So if we need to compare other datatypes like decimal, double and objects, then the code above would not work and we need to create another method to compare the proposed data type. We can solve this problem with generics.

Solution
Create a class as in the following code,
- class CompareGenericClass < T > {
- public bool Compare(T x, T y) {
- if (x.Equals(y)) return true;
- else return false;
- }
- }
Understanding the code
- We created the class CompareGenericClass with the input parameter T so the class is CompareGenericClass<T>
- Here T would be the datatype.
- If we want to compare strings then the following style would be used to create an object of the class,
- CompareGenericClass<string> Ocompare = new CompareGenericClass<string>();
- bool stringResult=Ocompare.Compare("DEVESH", "DEVESH");

- Since we passed T as a string the Compare method will accept only a string type of parameter.
- We did the same for Interger as well.
- CompareGenericClass<int> oIntcompare = new CompareGenericClass<int>();
- bool integerresult=oIntcompare.Compare(5, 6);

- Here with this class we do not need to overload the Compare method because this compare method accepts only a parameter that has been passed during creation of class objects.
- Using this class we can avoid such problems that have been discussed above.


Dilshad AshrafPosted Mar 21, 2021, 4:00 PM
Nice explanation...Thanks for sharing...
Md HussainPosted May 26, 2020, 4:45 AM
Good explanation Thank you
Juan PriottiPosted Jun 10, 2019, 9:33 AM
Good one, thanks for sharing!
Ankur MistryPosted Jun 9, 2019, 11:02 PM
Thanks for sharing...
Hemalatha SunnapuPosted Nov 22, 2018, 7:26 AM
I want reply for my comment.........
Hemalatha SunnapuPosted Nov 22, 2018, 7:24 AM
We can call a method with different data types by just declaring actually parameters as dynamic..............
Karthik ElumalaiPosted Nov 2, 2017, 5:32 AM
Good and nicely explained
Rakesh reddyPosted May 5, 2017, 8:02 PM
Its very clear to understand about Generic classes. Thank you very munch
Hrishikesh KulkarniPosted Apr 26, 2017, 10:06 PM
Thanks Nice Example
Devesh OmarPosted Mar 11, 2017, 4:27 AM
Another blog: http://compilefactory.blogspot.in/2017/03/basics-of-generic-classes-in-c.html
Hemant BharadwajPosted May 30, 2016, 1:48 PM
Well Explanined
Pravin KalePosted Mar 29, 2016, 2:16 AM
Hi Devesh Omar, we can do same thing using following code .can you please explain use of generic classes. I don't know i'm wrong or right.This is the codepublic bool Compare<T1,T2>(T1 val1,T2 val2) { if (val1.Equals(val2)) return true; else return false; }
Lalit RaghavPosted May 16, 2015, 8:29 AM
Nice Article
Murugesh PPosted Nov 17, 2014, 2:03 AM
Thanks for the Simple and clear Example
Arun TPosted Jun 21, 2014, 12:37 PM
Nice article
Naveed ZamanPosted May 13, 2014, 5:30 AM
useful
Devesh OmarPosted May 2, 2014, 3:34 AM
Thanks Sir
Nimit JoshiPosted May 2, 2014, 3:10 AM
Very Good Explanation..
zeshan ajmalPosted May 2, 2014, 2:28 AM
nice article
Devesh OmarPosted May 2, 2014, 1:37 AM
Thanks Lakshmanan :)
Lakshmanan Sethu SankaranarayanPosted May 1, 2014, 12:09 PM
Good one