!Dan Vallejo's WebLog!

Visual Studio .NET Developer

C# 2.0 Generics

Visual Studio 2005 also introduces the next version of C#. C# 2.0 introduces generics, iterators, partial class definitions, nullable types, anonymous methods, the :: operator, static classes, accessor accessibility, fixed sized buffers (unsafe), friend assemblies, and #pragma warnings.

Generic types allow for the reuse of code and enhanced performance for collection classes (due to boxing and unboxing issues).

Generics are classes that are not type specific. For example, instead creating a Stack class for integers, another one for floats, you can create a generic class. The way this was done in the past was to create classes that just took objects. But, this lacks compile-time type checking. Everything is done at run-time.

// File: StackObject.cs
using System;
class Stack
{
    object[] stack;
    int top;
    public Stack(int size)
    {
        stack = new object[size];
        top = 0;
    }
    public object Pop()
    {
        return stack[--top];
    }
    public void Push(object v)
    {
        stack[top++] = v;
    }
}
class Program
{
    static void Main()
    {
        Stack stack = new stack(10);
        // Performance hit, boxing!!!
        stack.Push(5);
        stack.Push(10);
        // Another perf hit, unboxing!!!
        int top = (int)stack.Pop();
    }
}

By using generics, you can eliminate the boxing and unboxing.

// File: StackGeneric.cs
using System;
class Stack<T>
{
    T[] stack;
    int top;
    public Stack(int size)
    {
        stack = new T[size];
        top = 0;
    }
    public T Pop()
    {
        return stack[--top];
    }
    public void Push(T v)
    {
        stack[top++] = v;
    }
}
class Program
{
    static void Main()
    {
        Stack<int> intStack = new Stack<int>(10);
        intStack.Push(5);
        intStack.Push(5);
        int top = intStack.Pop();
    }
}

 

Published Thursday, October 14, 2004 3:37 PM by DanVallejo
Filed under:

Comments

 

blog.dreampro said:

October 14, 2004 8:15 PM
 

Wes said:

I have a question for you. You may not be the right person to be asking this too but I will try anyways.

The other day I posted http://weblogs.asp.net/whaggard/archive/2004/10/12/241476.aspx about having a generic type inherit from Attribute. Do you know why this is prohibited in C#?

Thanks for your reply.
Wes
October 14, 2004 5:31 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker