.NET Generics

I was watching this video last night. Out of many things one feature they will introduce in Whidbey is Generics. Although this sounds like a NEW technology and it’s obvious similar sounding word: Genetics; it is far from that.

Generics is nothing but templates which has been around in C++ for several years now. Templates allow you to create a class/function that can act on several data-types at the same time.

For instance, if you wrote a class called: Adder and you wanted this function to add integer, float and strings you have 1 of two options in .NET :

1) Write three different classes: AddInteger, AddDouble, AddStrings
2) Write several overloaded functions within the class Adder
for instance:

string Add(string, string), double Add(double, double), int Add(int, int)

With generics or templates you can write one body and bind the data-type at run-time. Meaning the compiler will not know what datatype Add is going to work on until at run-time.

I will write more on this subject soon!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Live
  • StumbleUpon


Leave a Reply