I would like to share the basics of generics. Here first we will learn the problem statement and then resolve the problem using Generics.
First create a class as in the following code.
csharpclass 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; } }
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.
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.
Create a class as in the following code.
csharpclass CompareGenericClass < T > { public bool Compare(T x, T y) { if (x.Equals(y)) return true; else return false; } }
1. We created the class CompareGenericClass with the input parameter T so the class is CompareGenericClass<T>
2. Here T would be the datatype.
3. If we want to compare strings then the following style would be used to create an object of the class,
csharpCompareGenericClass<string> Ocompare = new CompareGenericClass<string>(); bool stringResult=Ocompare.Compare("DEVESH", "DEVESH");
4.Since we passed T as a string the Compare method will accept only a string type of parameter.
5. We did the same for Integer as well.
csharpCompareGenericClass<int> oIntcompare = new CompareGenericClass<int>(); bool integerresult=oIntcompare.Compare(5, 6);
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.
7. Using this class we can avoid such problems that have been discussed above
Output of code
csharpclass 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; } } class CompareGenericClass < T > { public bool Compare(T x, T y) { if (x.Equals(y)) return true; else return false; } } class Program { static void Main(string[] args) { CompareClass obj = new CompareClass(); bool intresult = obj.Compare(5, 7); Console.WriteLine("int comapre result:" + intresult); bool stringresult = obj.Compare("DEVESH", "DEVESH"); Console.WriteLine("string comapre result:" + stringresult); CompareGenericClass < string > Ocompare = new CompareGenericClass < string > (); bool stringResult = Ocompare.Compare("DEVESH", "DEVESH"); Console.WriteLine("Generic string comapre result:" + stringResult); CompareGenericClass < int > oIntcompare = new CompareGenericClass < int > (); bool integerresult = oIntcompare.Compare(5, 6); Console.WriteLine("Generic int comapre result:" + integerresult); } }
We have learned how to use a Generic class and why we need to use it.
Copyrights © 2024 letsupdateskills All rights reserved