Generics in C#

I can’t tell you how excited I was to find out that C# for .Net 2.0 was going to include generics. In part, my dissapointment with it is probably from overly high expectations.  C++ Template programming was something that facinated me deeply whilst I was at university and the sheer power of C++ Templates to do things that I personally never would have even thought of – everything from compile-time asserts to code-generating libraries such as the Standard Template Library (STL).

There are some major differences between C++ templates and the C# implementation of generics however.  The most important being that type substitution now takes place at runtime, rather than compile time. This elimates the entire section of templates that originally caught my attention as much of the point behind that particular brand of coding was to eliminate unnecessary runtime calculations by forcing the compiler to do it for you where possible.  C# Also doesn’t support either total or partial specialisation – one of Alexandrescu’s primary tools for his amazing C++ templates.

There are plenty of other differences between them, but the question then to ask is what good are C# generics if I can’t do that stuff? Well, I don’t have to use arraylist anymore – I finally have List<>, a strongly typed version of arraylist.  And that by itself is worth a lot to anyone developing reuseable components.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s