Thursday, April 5, 2012

C# generics with primary datatype

Recently me and my friend discuss about the Generics in C# after long discussion we agreed on one common definition. which is as below.
Generic is feature which is useful when you have set of types which is going to perform some set of the functions which are same but the output differ from one type to another type.
or alternatively
is way of saying "whatever the type is, I want to be able to do some common set of operation(s)." The implementation of the operation is different, but the idea is the same.
To understand this thing properly I created one console application in which we created one method which do the addition as below.
public T Add<T>(T t1, T t2)
{
        T t3 = t1 + t2;
        return t3;
}
so as per the generic definition this should work but when I complied, I got following compile time error.
Error 1 Operator '+' cannot be applied to operands of type 'T'.
First when we encounter error I think that might be I did something wrong. After sometime I realise the thing that what ever you passed as generic types its a object. So if I pass any primary data type it first get boxed automatically and than generic function take care of that. By following this fact if we apply the +,- etc. kind of arithmetic operation on the primary dataype in the generic function not going to work because its get converted in object i.e boxed.
To resolve the problem with the primary datatype in generic function I code my above function as below.
public T Add<T>(T val1, T val2)
    {
        if (val1 == null)
            throw new ArgumentNullException("val1");
        if (val2 == null)
            throw new ArgumentNullException("val2");

        object n1 = val1,
               n2 = val2;

        if (val1 is byte)
            return (T)((object)((byte)n1 + (byte)n2));
        if (val1 is short)
            return (T)((object)((short)n1 + (short)n2));
        if (val1 is int)
            return (T)((object)((int)n1 + (int)n2));
        if (val1 is long)
            return (T)((object)((long)n1 + (long)n2));
        if (val1 is float)
            return (T)((object)((int)n1 + (int)n2));
        if (val1 is double)
            return (T)((object)((double)n1 + (double)n2));

        throw new InvalidOperationException("Type " + typeof(T).ToString() + " is not supported.");

    }
As you can see in above code first I check the type of the object and if the type of object match than I perform the function that do the add operation. Basically I am unboxing parameter passed to the function.

Conclusion
In generic primary datatype treated as boxed object so when you are coding generic for primary datatype beware that you need to convert passed parameter in primary datatype i.e you need to unbox the values again.

No comments:

Post a Comment