Saturday, February 4, 2017

What is generic?

Generic class allows to write such a class which is data type independent and its method can work with any data type.

Life without generic

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;
    }
}

Life with generic

class CompareGenericClass<T>
{
    public bool Compare(T x, T y)
    {
        if (x.Equals(y))
        return true;
        else
        return false;
    }
}

No comments:

Followers

Link