// Generics in c#
using System;
using System.Collections.Generic;
using System.Text;
namespace csharpgenerics
{
class Stack<T>
{
private T[] array;
private int index;
public int Max_Size = 50;
public Stack()
{
array = new T[Max_Size];}
public T Pop()
{
if(index == 0)
throw new System.InvalidOperationException("Stack is emply");
return array[--index];
}
public void Push(T item)
{
if(index == Max_Size)
throw new System.StackOverflowException("Stack is full");
array[index++] = item;
}
}
class Generics
{
static void Main(string[] args)
{
Stack<int> stack = new Stack<int>();
stack.Push(1);
int num = stack.Pop();
stack.Push(5);
//Below line will not work because you opened the stack object as interger
// string numNext = stack.Pop();
// to work above lines you need to open object as string similar with
// other user types.
/*
Stack<string> stack = new Stack<string>();
stack.Push("1");
string num = stack.Pop();
stack.Push("5");*/
}
}
}