Sharing the information you need to get development done!
Welcome to DevAuthority.Com Sign in | Join | Help
in Search

be-#

Why use Generics in .net?

I noticed a posting on Generics on the main page, and I thought I would add my own 2 cents. Hopefully this will help clear up what Generics are, how they are different from derived classes, and when a designer might choose to use them over derived classes.

First, a definition: Generics are nothing more than a class template, into which the compiler will drop whatever type(s) you like (within the generic definition's constraints, of course). In other words, roughly put, instead of cutting and pasting to create several functionally identical classes whose only difference is the types they operate on, let the compiler do the "cutting and pasting" for you.

So, when would one use generics as opposed to, say, derived classes? Here's a simple example.

Let's say that you have separate FIFO queues, each of which handle different types.

You could define a class that simply stores the queue items as objects. But these are not type-safe, you would need to do casting, and there's that boxing/unboxing problem for value types. Plus the overhead incurred from the compiler having to maintain virtual method pointer tables (with the caveat that I remember this from my old C++ compiler days, it's possible the .net runtime has optimized this).

So you create derived classes from the base class that each are of the required type. So that buys you type-safety and eliminates casting, but it doesn't help you with the boxing/unboxing of value types, which would still be stored as an Object reference in the base class. Worse, if there are any other methods that need to be type-safe, which there almost certainly will be, then you have to duplicate (cut/paste/modify) each of those methods in each of your derived classes. So we're starting to get pretty far away from "code reuse."

Generics promise full code reusability.  You write the code once, and literally drop in the types that the class will operate on when you instantiate the class.  Type-safe, fast, flexible, extensible, and fully reusable.

Published Wednesday, October 10, 2007 12:22 PM by ebuatois

Comments

 

Confessions of a Webgypsy said:

Let me be the first to welcome Edward to DevAuthority.com.  I am thrilled to have you on board (even...
October 10, 2007 7:43 PM
 

jkimble said:

You are relying a little too heavily on your C++ background.  I think (I'm a little unclear somewhat on your post).

Here's the deal.  Generics in .NET are better than C++ templates.  They are real classes.  Think of it as you have the ability to control that object type in the base class... you can actually set it to whatever you want.  the boxing/unboxing issues go away because behind the scenes you are actually storing as the tempated type.

I believe that the .NET runtime actually does something similar to CodeGen so it actually creates a new type from scratch with the underlying type filled in.  This creates a much more performant situation.  

Also a type of List<string> is totally unrelated to a List<Int32>.  With the exception that they use the same generic to generate their type (Yeah, I know that might be somewhat incorrect language), they are 2 totally different types (and there's no way you could say cast one to the other).

Jay

PS. I hope you're coming tonight...
October 15, 2007 8:03 AM
 

ebuatois said:

Hey, Jay!  Thx for your presentation on Ajax/Silverlight last night -- I learned a lot!

Sorry you were unclear on some of what I wrote.  Partly, it didn't format the way I expected.  Anyway, judging from your comments, I actually think we're on the same page.

The only difference I have with your comment may only be a semantic one, especially since I'm not familiar with the recent incarnations of C++, but I would not agree that generics are "real classes."  Here's why I say that.  To me, a class compiles directly to code.  In the case of a template, the compiler has to perform the extra step of dropping in the types before compiling the code.

Put another way, with a non-generic class the functionality can be fully known just by looking at the code and/or method definitions -- one doesn't have to look at where the class is actually used and/or instantiated.  In the case of a generic template, one doesn't fully know the functionality of the template unless one also looks at where the template is used and/or instantiated.
October 16, 2007 8:34 AM
 

DevPrime said:

That's correct. A generic "class" doesn't really compile to a class, and that's part of the reason there is no variance between generic-derived classes (from the same template) with different type parameters. For all intents and purposes, a generic "class" really is a template that the compiler and CLR use to play a game of fill-in-the-blanks with the type parameters. Having said that, there are a few differences between the way .NET handles generics and how C++ handles templates. The issue of invariance is one of them. Also, in C++ if include the same template in multiple projects, and use the same type parameters in each, you will get completely different classes compiled. The CLR handles this at runtime to some extent, and will match up classes derived from the same generic template in different assemblies as the same class. For example, List<int> is the same class no matter what assembly it is defined in.
October 30, 2007 12:51 PM
Anonymous comments are disabled







This Blog

Post Calendar

<October 2007>
SuMoTuWeThFrSa
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Syndication



Powered by Community Server, by Telligent Systems