Tuesday, 21 August 2018

C#: Generics

Generics are introduced in C# 2.0. They are used to make your code type independent. Generics are extensively used by collection classes. First let us try to understand the usage of generics.

Suppose, you want to write a method, where you compare the two objects of same type and return true, if they are equal, else false.

    public static bool isEquals(String s1, String s2)
    {
        return s1.Equals(s2);
    }

Suppose you have to implement same equality check for int, float also. You will implement like below.

    public static bool isEquals(int s1, int s2)
    {
        return s1.Equals(s2);
    }

    public static bool isEquals(float s1, float s2)
    {
        return s1.Equals(s2);
    }

If there are ‘n’ different data types, to perform equality checks you need to implement ‘n’ different methods. As you closely observe the above methods, we are just calling Equals method on data types, nothing else. By using generics, we can define one generic method that is used to handle all the data types, means we are defining the code as type independent.

Following is the generic method implementation for isEquals method. ‘T’ represents some type, it can be string, int, float, custom type (or) any other object.

    public static bool isEquals<T>(T s1, T s2)
    {
        return s1.Equals(s2);
    }

Following is the complete working application.

Program.cs

using System;
using System.Reflection;

class EqualityChecker
{
    public static bool isEquals<T>(T s1, T s2)
    {
        return s1.Equals(s2);
    }
}

class Program
{

    static void Main()
    {
        String s1 = "Hello World";
        String s2 = "Hello";
        String s3 = "Hello";

        bool result1 = EqualityChecker.isEquals(s1, s2);
        bool result2 = EqualityChecker.isEquals(s2, s3);

        Console.WriteLine("result1 : {0}", result1);
        Console.WriteLine("result2 : {0}", result2);

        int i1 = 10;
        int i2 = 11;
        int i3 = 11;

        bool result3 = EqualityChecker.isEquals(i1, i2);
        bool result4 = EqualityChecker.isEquals(i2, i3);

        Console.WriteLine("result3 : {0}", result3);
        Console.WriteLine("result4 : {0}", result4);
    }
}


Output

result1 : False
result2 : True
result3 : False
result4 : True

Defining Generic class
It is similar to how we define generic method. Just add the type variable <T> to the class.

class EqualityChecker<T>
{
    public static bool isEquals(T s1, T s2)
    {
        return s1.Equals(s2);
    }
}

Following is the complete working application.


Program.cs

using System;

class EqualityChecker<T>
{
    public static bool isEquals(T s1, T s2)
    {
        return s1.Equals(s2);
    }
}

class Program
{

    static void Main()
    {
        String s1 = "Hello World";
        String s2 = "Hello";
        String s3 = "Hello";

        bool result1 = EqualityChecker<String>.isEquals(s1, s2);
        bool result2 = EqualityChecker<String>.isEquals(s2, s3);

        Console.WriteLine("result1 : {0}", result1);
        Console.WriteLine("result2 : {0}", result2);

        int i1 = 10;
        int i2 = 11;
        int i3 = 11;

        bool result3 = EqualityChecker<int>.isEquals(i1, i2);
        bool result4 = EqualityChecker<int>.isEquals(i2, i3);

        Console.WriteLine("result3 : {0}", result3);
        Console.WriteLine("result4 : {0}", result4);
    }
}

Output

result1 : False
result2 : True
result3 : False
result4 : True



Previous                                                 Next                                                 Home

No comments:

Post a Comment