Geeks With Blogs
Neil Smith [blog]code til ur fingers bleed

I’ve written a windows exe which I think highlights the advantages of using generics, the sample features a stack type class called nsStack, the source code for which is :-


namespace nsGenerics


    class nsStack


        private T[] m_ItemsArray;

        private int m_Index = 0;

        public const int MAX_SIZE = 5;

        public nsStack()


            m_ItemsArray = new T[MAX_SIZE];



        public T Pop()


            if (m_Index == 0)

                throw new System.InvalidOperationException("nsStack is empty.");

            return m_ItemsArray[--m_Index];


        public void Push(T item)


            if (m_Index == MAX_SIZE)

                throw new System.StackOverflowException("nsStack is full.");

            m_ItemsArray[m_Index++] = item;





I then add a form with two buttons on it, the code on the click of the first button is :-


private void button1_Click(object sender, EventArgs e)


    nsStack<int> stack = new nsStack<int>();


    int number = stack.Pop(); // Don't need any awkward cast.


    //string sNumber = stack.Pop();  // Compilation Error:

    // Cannot implicitly convert type 'int' to 'string'.


    int iNumber = stack.Pop();  // Works OK!



The code under the second button is :-


private void button2_Click(object sender, EventArgs e)


    nsStack<string> stack = new nsStack<string>();


    string number = stack.Pop(); // Don't need any awkward cast.


    //int iNu = stack.Pop();  // Compilation Error:

    // Cannot implicitly convert type 'int' to 'string'.

    string sPopped = stack.Pop();  // Works OK!



Button1 creates nsStack with integers, if you uncomment the string sNumber = stack.Pop(); line of code, Visual Studio will not even compile the project and will produce a ‘Cannot implicitly convert’ error.  These elements are actually stored in memory as integers which means no boxing or unboxing ever occurs with non primitive types.  This is excellent news, in previous versions of vs, for me, the most common error was a runtime type conversion.


Button2 demonstrates the same thing but with strings, the complete Visual Studio 05 project can be downloaded from here.




Other .net ramblings….


I thought it might be a while before we got another major release of the framework from Microsoft but apparently not.  The .net framework 3 is what used to be called WinFX and comprises the Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), Windows Workflow Foundation (WF) and the newly christened Windows CardSpace (WCS) formerly known under the codename InfoCard. 


It is going to ship with Windows Vista though there is no mention anywhere of a new accompanying Visual Studio.  It sounds like the already sizeable dotnetfx.exe is about to get substantially bigger again.  I do hope it includes some sort of ‘only install parts of the framework you need’ functionality.


For a live ASCII feed of World Cup 2006, go to a dos prompt and type :-


telnet 2006

Posted on Thursday, June 15, 2006 9:41 AM | Back to top

Comments on this post: C# Generics Simplified

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Neil Smith | Powered by: