Blog Stats
  • Posts - 104
  • Articles - 0
  • Comments - 162
  • Trackbacks - 383

 

First Go at Generics! (C# 2.0)

I recently installed Visual Studio Whidbey Beta 1 (which i received through MS Community Starter KIT) on my machine. It is kind of a cool ide with lots of enhancements like Refactoring and Expansions.  I wrote my first program implementing the concepts of Generics. Generics are a new feature of .Net Framework 2.0. With Generics we can defer type specification of one or more type until the class is declared and instantiated by the client code. This effectively translates into Templates Concept of C++.

I implemented a generic stack class and the code goes like this:

#region Using directives

using System;

using System.Collections.Generic;

using System.Text;

#endregion

namespace helloGenerics

{

#region Stack Class

/// <summary>

/// My Simple Stack Class using Generics

/// </summary>

/// <typeparam name="T">Type Parameter</typeparam>

public class Stack<T>

{

/// <summary>

/// Top of the Stack

/// </summary>

private int top;

/// <summary>

/// Generic Collection to hold stack contents

/// </summary>

private System.Collections.Generic.Collection<T> data;

/// <summary>

/// Stack Constructor

/// </summary>

/// <param name="t"></param>

public Stack(System.Collections.Generic.Collection<T> t)

{

data = t;

top = 0;

}

/// <summary>

/// Default Constructor

/// </summary>

public Stack()

{

data = new System.Collections.Generic.Collection<T>();

top = 0;

}

/// <summary>

/// Push

/// </summary>

/// <param name="item">Item/Content to be pushed</param>

public void push(T item)

{

data.Insert(top,item);

top++;

}

/// <summary>

/// POP

/// </summary>

/// <returns>Content / Item on stack top</returns>

public T pop()

{

if (top > 0)

{

top = top - 1;

return data[top];

}

else

{

throw new IndexOutOfRangeException(" Stack is already empty");

}

}

}

#endregion

#region Testing Stack Class

/// <summary>

/// Testing Stack Class

/// </summary>

class Program

{

static void Main(string[] args)

{

// Stack Class Object -- See char as type paramter.. (this reminds me of C++ :))

Stack<char> myStack = new Stack<char>();

myStack.push('H');

myStack.push('A');

myStack.push('M');

Console.WriteLine(myStack.pop().ToString());

Console.WriteLine(myStack.pop().ToString());

Console.WriteLine(myStack.pop().ToString());

// Index Out of Bound Exception

Console.WriteLine(myStack.pop().ToString());

Console.ReadLine();

}

}

#endregion

}


 

This code wont get me any accolades, that I know :). But it still proves the point that Generics are simple and very useful in terms of re-usable data structures.

Hammad Rajjoub.

UG Leader and Member Speakers Bureau,
Ineta Pakistan.
http://dotnetwizards.blogspot.com


Feedback

# re: First Go at Generics! (C# 2.0)

Gravatar The declaration and usage seems to be wrong here... 11/1/2004 6:19 PM | Zubin

# re: First Go at Generics! (C# 2.0)

Gravatar Hello Zubin,
Can you please specify where the "Declaration and Usage" seems to be "wrong". I have compiled this code using VS Whidbey Beta 1.
best regards,
Hammad.Rajjoub. 11/2/2004 9:07 AM | Hammad Rajjoub

# re: First Go at Generics! (C# 2.0)

Gravatar Zubin comment is correct,
generic usage suppose to look somthing like that,

public class MyStack<ItemType> {

public MyStack(ItemType t) {
...
}

public void Push(ItemType item){
...
}

public ItemType Pop(){
...
}

} 11/21/2004 8:37 PM | Haim

# re: First Go at Generics! (C# 2.0)

Gravatar You are right Zubin and Haim, I believe i posted the wrong code snippet. How can a generic class be declared without "<>" . I wrote the code at my home pc. I will update it as soon as i get back to home.
Thanks for pointing out.... sorry for the inconvenience 11/22/2004 8:34 AM | Hammad Rajjoub

# re: First Go at Generics! (C# 2.0)

Gravatar I have updated the code :)

http://blogs.ineta.org/pakistan/archive/2004/10/18/12793.aspx
11/28/2004 12:03 PM | Hammad Rajjoub

Post a comment





 

 

 

Copyright © INETA Pakistan