C#, CLI (.NET), and C++/CLI Standardization
by Rex Jaeschke

Example 1: 

(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 2

public static class Math
{
   public const double PI = 3.14159265358979;
   public static double Sin(double d) {...}
   public static double Cos(double d) {...}
   public static double Tan(double d) {...}
   ...
}

Example 3:

// source file #1
partial class Window
{
   ...   
}
// source file #2
partial class Window
{
   ...   
}


Example 4

(a)
public delegate bool Del(int value);
Del d = delegate(int val) {
   ...
   return true;
};

(b)

public delegate bool Del(int value);
static void F(Del d) { ... }
F(delegate(int val) {
   // ...
   return false;
});


Example 5:

using System;
using System.Collections.Generic;
class Test
{
   static IEnumerable<int> FromTo(int from, int to) {
      while (from <= to)
         yield return from++;
   }
   static void Main() {
      IEnumerable<int> e = FromTo(1, 10);
      foreach (int x in e) {
         foreach (int y in e) {
            Console.Write("{0,3} ", x * y);
         }
         Console.WriteLine();
      }  
   }
}

Example 6:

int* pi = new int;
int^ hi = gcnew int;

Example 7:

void f(array<double, 2>^ array2D)
{
   array2D[3,1] = 1.23;
}
int main() {
   array<int>^ array1D = gcnew array<int>(5);
   array1D[2] = 5;
     f(gcnew array<double, 2>(5, 10));
}


Example 8: 

ref class C {
   literal int Min = 10;
public:
   literal int Max = Min + 100;
};
int main() {
   Console::WriteLine(C::Max);
}

Example 9:

(a)
ref class X abstract {...};
ref class Y sealed {...};

(b)
ref struct X {
   virtual void f() abstract;
   virtual void f() new {...}
   virtual void f() override {...};
   virtual void f() sealed {...}
};


(c)
struct A {
   virtual void f() abstract;
};
struct B {
   virtual void f() abstract;
};
struct D : A, B {
   virtual void f(); // overrides A::f and B::f
};
struct E : A, B {
   virtual void g() = B::f; // overrides B::f only, E is abstract
};
struct F : A, B {
   virtual void f() override; // overrides A::f and B::f
};


Example 10:

(a)
Queue<double>^ q = gcnew Queue<double>;
for (int i = 1; i <= 5; ++i)
   q->Enqueue(i * 2.5);
for each (double d in q)
   Console::Write(" {0}", d);
Console::WriteLine();
array<int>^ values = {4, 6, 7, 9, 2};
for each (int value in values)
   Console::Write(" {0}", value);
Console::WriteLine();


(b)
2.5 5 7.5 10 12.5
4 6 7 9 2

Example 11:

(a)
public enum class Color : short {Red, Green = 12, Blue, Yellow};
int main() {
   Color c = Color::Green;
   Console::WriteLine("Color.Green's name is {0}",
      Enum::GetName(c.GetType(), Color::Green));
   Console::WriteLine("Color's members are:");
   array<String^>^ names = Enum::GetNames(Type::GetType("Color"));
   for each (String^ str in names) {
      Console::WriteLine(str);
   }
   Console::WriteLine("The type underlying Color is {0}",
      Enum::GetUnderlyingType(typeid<Color>));
}

(b)
Color.Green's name is Green
Color's members are:
Red
Green
Blue
Yellow
The type underlying Color is System.Int16


Example 12:

(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);






5



