Generc Stack Class C# with Example
using System; namespace CSharpGenerics { class MyStack { T[] StackArray; int TOS = 0; public void Push(T x) { if ( !IsStackFull ) StackArray[TOS++] = x; } public T Pop() { return ( !IsStackEmpty ) ? StackArray[--TOS] : StackArray[0]; } const int MaxStack = 10; bool IsStackFull { get{ return TOS >= MaxStack; } } bool IsStackEmpty { get{ return TOS <= 0; } } public MyStack() { StackArray = new T[MaxStack]; } public void Print() { for (int i = TOS-1; i >= 0 ; i--) Console.WriteLine(" Value: {0}", StackArray[i]); } } class UsingGenericClass { static void Main( ) { MyStack StackInt = new MyStack(); MyStack StackString = new MyStack(); Console.WriteLine("Integer Stack: "); StackInt.Push(3); StackInt.Push(5); StackInt.Push(7); StackInt.Push(9); StackInt.Print(); Console.WriteLine("\nString Stack: "); StackString.Push("Kuldeep Mishra"); StackString.Push("Raj Kumar Sharma"); StackString.Print(); } } }