Home » none » How Generics Work

How Generics Work

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.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it […]
  • Dispose, Finalize and SuppressFinalizeDispose, Finalize and SuppressFinalize I got the following question recently. What is the difference between Dispose and SupressFinalize in garbage collection?” The problem with this question is it assumes Dispose and […]
  • Document/View All Over Again…Document/View All Over Again… Back when we programmed in C++ using the Microsoft Foundation Classes (MFC) we had a framework that we programmed in called "Document/View." The question I keep asking myself as I think […]
  • CSharp adds the var keyword!CSharp adds the var keyword! There have been several new features added to the CSharp language that will significantly reduce the amount of code that ends up in our source files.  It will not significantly reduce the […]
  • .NET Image Scaling in CSharp.NET Image Scaling in CSharp One feature of .NET that I use regularly is image scaling.  I typically use this on web sites that need image upload capabilities.  I assume the user is going to send me an […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

  • http://www.msnetprogrammer.net/blog volkan uzun

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

  • ramil

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