.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

How Generics Work

June 16, 2008 By: Dave

One of my fundamental beliefs about programming is that the best programmers understand what’s actually happening under the hood. This is why it is beneficial to look at the code for the .NET framework. Once you understand what’s going on, you have a better understanding of what you can and cannot do. Generics are one of those language features where it helps to understand what’s happening under the hood in order to use the feature to its full potential. To fully understand, we need a bit of a history lesson, so we need to visit our friend C++.

In C++, Generics are called Templated Classes. I think we would have been better off keeping that name since it more accurately represents what is going on.

In C++, Templated Classes are actually handled by the preprocessor, the same thing that resolves the #DEFINE statements and the #INCLUDE statements. What it actually does is create a brand-new class based on the type parameters it is passed.

Once the class has been created by the preprocessor, it is compiled to binary.

While Generics may not actually have the same mechanics (preprocessor, compile) the effect is the same. You are defining a brand-new class when you specify a generic with the type information in the <> parameters.

So, a class defined as:

class GenericText<T>
{
    void foo1(T param1)
    {
    }

    T foo2()
    {
        T v;
        return v;
    }

}

Called using:

var s = new GenericText<string>();

Actually becomes something like:

class GenericTextString
{
    void foo1(string param1)
    {
    }

    string foo2()
    {
        string v;
        return v;
    }

}

and when called like this:

var s = new GenericText<int>();

Becomes this:

class GenericTextInt
{
    void foo1(int param1)
    {
    }

    int foo2()
    {
        int v;
        return v;
    }

}

The name of the class is actually something system-generated, but the class definition is the same as what I’ve represented above.

Since this all happens at compile time, and not at runtime, it would be difficult to use this as a way of generating a class factory. For that, we would need some other syntax, and would need a base class that defined our generic class interface that we wanted to call.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

  • Using VB.NET From CSharp
  • iTextSharp – Adding Images
  • Hungarian Notation – Use What Works, Spit Out The Bones
Bookmark to:

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
Hide Sites
Tags: csharp, generics

2 Responses to “ How Generics Work ”

  1. # 1 volkan uzun Says:
    June 16th, 2008 at 2:36 pm

    nice article, thanx.
    now the next one should be limitations of T

  2. # 2 ramil Says:
    June 16th, 2008 at 9:43 pm

    yes nice article, you don’t have to rewrite another class for each type that will be stored. tnx!

← DotNetNuke Skins – Handling CSS Files
DotNetNuke Skins – Hello, World →
  • Search

  • Subscribe

    U COMMENT
    I FOLLOW

    Subscribe in a reader

    OR

    Subscribe via e-mail

    Enter your email address: 

    Delivered by FeedBurner

     

  • Follow Me

    • Twitter
    • FaceBook
    • Digg
    • StumbleUpon
    • Propeller
    • Delicious
    • Plaxo

     

  • Recent Posts

    • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
    • jQuery – Date Picker
    • Using VB.NET From CSharp
    • iTextSharp – Adding Images
    • Hungarian Notation – Use What Works, Spit Out The Bones
    • Pre Order Windows 7
    • jQuery Dialog – With Validation Controls
    • iTextSharp – The easy way
    • Structure of my ASP.NET Web Applications
    • 35% Off Accronis True Image 2009 Home
    • VB.NET Hide Module Name
    • ASP.NET/VB.NET – Video Training
    • Does jQuery Make Us Lazy?
    • PDFs Using iTextSharp
    • Programming SEO – Ping



  • Advertise on this site through Lake Quincy Media
  • DotNetNuke Sponsor

     

    Most Valuable Blogger
  • Sponsor

  • Categories

    • Advanced CSharp
    • Advanced VB.NET
    • ASP.NET MVC
    • Did you know
    • DotNetNuke – Module Development
    • DotNetNuke – Skinning
    • internationalization
    • iTextSharp
    • jQuery
    • none
    • Seach Engine Optimization
    • Silverlight
    • SQL For Programmers
    • Twitter
    • winforms
  • Cloud

    .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • Privacy Policy
  • Calendar

    June 2008
    S M T W T F S
    « May   Jul »
    1234567
    891011121314
    15161718192021
    22232425262728
    2930  
  • Blogroll

    • Alvin Ashcraft’s Morning Dew
    • ASP.NET Consulting
    • Life Hacker
    • Remember Anything
    • The Price of Their Toys
    • Uncategorized Thought


.NET Answers © 2007 - 2008 All Rights Reserved.
Entries and Comments.