I find that when describing technology, people tend to use obscure terminology wherever possible.  This normally scares people off and I try to avoid that in my articles.  What I try to do is cover the basic principals which will hopefully make sense to you and then inspire you to go and research the wonderful MSDN resource for a more comprehensive view.

Today I'm on the topic of Generics.  This was a feature added in C# 2.0.

Generics help you save writing code.  One way they do this is by allowing you to create data structures (classes) that have no specific data type (IE: they’re generic).  Generics are a great tool and you can really put them to good use (I promise!)

So let’s say you needed to hold a collection of integers in a queue.  In C# 1.1 you could do something like this:

public class IntQueue
{
    private int[] queue;
    private int queueHead = 0;
    private int queueTail = 0;

    public IntQueue(int maxElements) { this.queue = new int[maxElements + 1]; }

    public void Enqueue(int item)
    {
        this.queue[queueTail++] = item; // add an item to the back of the queue

    }

    public int Dequeue()
    {
        return queue[queueHead++]; // remove the first item in the queue
    }
}

(Please note that in the real world you would add error handling to the above)

This example would work just fine.  But the problem arises when you want to create a queue of something other than integers.

Let’s say you wanted a queue of strings.  The above example is useless as it only knows how to queue integers.  In C# 1.1 you would have to re-type the whole code again but with the word “string” in place of the word “int” where relevant. This is too much effort for the programmer, and too messy.

One thing that I find Microsoft are aiming for is finding ways to help you write less code.  Generics are no exception.  Generics let us define our queue so that it can be used for any datatype. 

How?  Fist, check out the modified queue class which takes advantage of generics:

public class IntQueue<DT>
{
    private DT[] queue;
    private int queueHead = 0;
    private int queueTail = 0;

    public IntQueue(int maxElements) { this.queue = new DT[maxElements + 1]; }

    public void Enqueue(DT item)
    {
        this.queue[queueTail++] = item; // add an item to the back of the queue
    }

    public DT Dequeue()
    {
        return queue[queueHead++]; // remove the first item in the queue
    }
}

You can see that all we modified was any relevant reference to “int” has been changed to <DT>.  What does <DT> mean?  This is the way to define a generic data type.  I have chosen to name it DT, however you can name it how you wish.

I just changed five words in our IntQueue source code and made it into a generic queue that can be used for any data type!  Let’s just take a minute to acknowledge that!

So, we have our generic queue, now how do we use it?  Well, first let’s see how to create a queue from our IntQueue class:

IntQueue myqueue = new IntQueue(5);

Now for the generic version:

IntQueue<int> myqueue = new IntQueue<int>(5);

That’s right, we just added the data type we wanted in the angle brackets.  And now for the string version:

IntQueue<string> myqueue = new IntQueue<string>(5);

C# 2.0 also allows for the creation of generic methods.  Imagine all of those utility classes that could come in handy with.  This is a topic that I will let you research.

I hope this gave you some introduction to Generics which would inspire you to research them further.  In your travels, make sure you check out Generic Methods, Multiple Generic Types, aliasing, and constraints.

I’m sure there are plenty of opportunities for you to use these in your own code.