(a)
public class Stack<ItemType>
{  public void Push(ItemType data) {...}
   public ItemType Pop() {...}
}

(b)
Stack<int> s1 = new Stack<int>(...);
s1.Push(100);
int x = s1.Pop();

(c)
Stack<Transaction> s2 = new Stack<Transaction>(...);
s2.Push(new Transaction (...));
Transaction t = s2.Pop();

(d)
public class MyDictionary<KeyType, ElementType >
   where KeyType: IComparable, IEnumerable
   where ElementType: Transaction
{
   ...
}

(e)
static void PushMultiple<ItemType>(Stack<ItemType> s,
                           params ItemType[] values)
{
      foreach (ItemType v in values) {
           s.Push(v);
   }
}

(f)
Stack<int> s3 = new Stack<int>(...);
PushMultiple<int>(s3, 1, 2, 3, 4);

Example 1: C# generics.

Back to Article