Tuesday, 21 February 2012

What are Generics in .NET?

A little while ago, a friend of mine on Facebook asked me to explain to him what generics in .NET were. As I thought this might interest more than one friend, which it did, I decided to put it in a "note" on Facebook. That was before I started this blog, so now I decided to post it here, hopefully so that more people would find it useful.

Generics in the .NET Framework is a programming technique that allows you to create a "generic" (meaning that it's not tied to a specific type) class, structure or method. The type, which is in this case "an argument", is specified when that element is used. What's interesting, and this is another advantage of Generics in .NET, is that you can restrict that type by specifying, for example, that it has to implement a certain interface or have a parameterless constructor.

This concept allows for more type safety (the type is known when the generic element is being used), while promoting reuse (you create an element that's not tied to a specific type).

An example of Generics, which is very popular, are Generic Collections, that came with the introduction of Generics in .NET 2.0. In the earlier version of .NET (1.x), there were non-generic collections, meaning that those collections would hold "Object" types, allowing the following:

ArrayList collection = new ArrayList();
collection.Add(new float[10]);

This may seem to have the advantage to be "flexible" but it has many disadvantages, like having to cast the types back when retrieving them:

int i= (int) collection[1];

And you have to know which element contains which type, and that can prove to be hard. Also, cases when you would need such a collection that can hold any "Object" aren't that common.
And here come the "Generics" to the rescue. You can use, for example, the List<T> to hold objects of type "T". e.g:

List<string> stringList = new List<string>();
stringList.Add(2); // this causes a compile-time error
string world = stringList[1]; // no need for the cast since the type is known

Another example of generics, other than in collections, would be something like:

int Compare<T>(T first, T second) where T:IComparable<T>
return first.CompareTo(second);

Now, this Compare method can compare any two objects (of the same type) which implement the IComparable interface (that defines the CompareTo method), which in turn makes for another good example of generics

public interface IComparable<T>
int CompareTo(T other);

Now, if you've not used generics before, maybe thinking they were hard, I hope you got a glimpse at how powerful this feature is and how useful it can be; and more importantly, how "not so hard after all" it is. So, what are you still doing here?! go make yourself some generics ;)


  1. I came onto your blog while focusing just slightly submits. Nice strategy for next, I will be bookmarking at once seize your complete rises... SEO company

  2. One of the most appealing and bewitching blog that I read recently.
    augmented reality advertising

  3. This allows them to go to class prepared. They also like using Skype, emailing their teachers instead of going to speak to them in person. They also enjoy discussion boards. wolffofcanada.com