Skip to main content

Basics of Generic Classes in C#

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
  1. We created class the CompareClass
  2. Here we created two compare methods, one for the string data type and the second for an int data type.
  3. 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
  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:

    CompareGenericClass<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 Interger as well.

    CompareGenericClass<int> oIntcompare = new CompareGenericClass<int>();         boolintegerresult=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

Complete 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;
    }
}
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);
    }
 
Conclusion
We have learned how to use a Generic class and why we need to use it.

Comments

Contact Form

Name

Email *

Message *

Popular posts from this blog

Exporting DataTable to Excel in C# Using Interop

Introduction I would like to share a utility that can be used to export a datatable to Excel file using C#. We have various approaches to perform this activity. In ASP.NET we can do this by changing the Content type to xls but in C# .Net we might have various other approaches. Through this article we will learn how to export a data table to Excel using Interop. Code and Steps Here in this application we will use a Sample datatable and then learn how to export data to an Excel file. We will learn the following things in this article: Creating Excel file using C# Writing data to cells Formatting data to cells Working with Excel range 1. Adding References First we need to add a reference for Microsoft.office.interop.Excel as in the following: 2. Adding sample data table to the code Use the following code to add the sample data table:     static   DataTable  GetTable()     {       ...

Binding CheckBoxList in ASP.Net MVC

Blogspot Introduction I would like to share how to bind the checkbox list in MVC. I will use a Model (a class file) to define various attributes for checkboxes. For a basic understanding of MVC kindly use the following link: ASP.NET MVC Overview The following is the procedure. 1. Creating Model We created a "SubjectModel" class under the Models folder and defined the following two properties: Subject: to display text Selected: to display check/uncheck 2. Creating the controller We created a Controller "BindingCheckBoxController" under the controllers folder We created an Action having the name = "DisplaycheckBoxes" We created a list of Subjects (Subject model class) Returning a list of subjects to the View. public  ActionResult DisplayCheckboxes() {     List<SubjectModel> listsubject =  new  List<SubjectModel>();     listsubject.Add( new  SubjectModel( "Physics" , true )); ...

Indexes In SQL SERVER in Hindi Part 1