(a)
generic<typename ItemType>
public ref class Stack
{
   ...
public:
   void Push(ItemType data) {...}
   ItemType Pop() {...}
};
int main() {
   Stack<int>^ s1 = gcnew Stack<int>(...);
      s1->Push(100);
      int x = s1->Pop();
}

(b)
generic<typename KeyType, typename ElementType>
   where KeyType: IComparable, IEnumerable
   where ElementType: Transaction
public ref class MyDictionary
{
   //...
};

(c)
generic<typename ItemType>
void PushMultiple(Stack<ItemType>^ s,
   ... array<ItemType>^ values)
{
      for each (ItemType v in values) {
         s->Push(v);
      }
}

(d)
Stack<int>^ s = gcnew Stack<int>(...);
PushMultiple<int>(s, 1, 2, 3, 4);

Example 12: C++/CLI generics

Back to Article