Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

C# Generics with primary datatypes

, 6 Feb 2013
Rate this:
Please Sign up or sign in to vote.
C# Generics with primary datatypes.

Recently me and my friend discussed about Generics in C# and after a long discussion we agreed on one common definition, which is as below: 

Generics is a feature which is useful when you have a set of types going to perform some set of functions which are the same but the output differs from one type to another type.

Alternatively, we can say "whatever the type is, I want to be able to do some common set of operation(s)." The implementations of the operation are different, but the idea is the same. To understand this properly I created a console application in which I created a method which does 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 the following compile time error:

Error 1 Operator '+' cannot be applied to operands of type 'T'.

First when I encountered the error I thought that it might be that I did something wrong. After sometime I realised that whatever you passed as a generic type it is converted to an object. So if I pass any primary data type it first gets boxed automatically and than the generic function take care of that. By following this fact if we apply +,- etc. kind of arithmetic operations on a primary data type, the generic function is not going to work because it gets converted to object, i.e., it is boxed.

To resolve the problem with the primary data types in generic functions I modified 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 the above code, first I check the type of the object and if the type of the object matches then I perform the function that does the add operation. Basically I am unboxing the parameter passed to the function.

Conclusion

In generics, primary data types are treated as boxed objects so when you are coding generics for primary data types beware that you need to convert the passed parameter in the primary data type, i.e., you need to unbox the values again.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

 
Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 

You can visit my blog

StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 4 PinmemberAbwaanka9915-Sep-13 3:53 
GeneralMy vote of 1 PinmemberJohn Brett6-Feb-13 23:04 
GeneralMy vote of 2 PinmemberKlaus Luedenscheidt6-Feb-13 19:36 
BugYour analysis of the difficulty is incorrect PinmemberMatt T Heffron6-Feb-13 12:46 
Suggestionusing dynamic keyword PinmemberHojjat Khodabakhsh7-Nov-12 19:21 
GeneralRe: using dynamic keyword Pinmemberwanjp20108-Nov-12 14:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 6 Feb 2013
Article Copyright 2012 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid