Demonstrate a generic struct : Generic struct « Generics « C# / C Sharp






Demonstrate a generic struct

Demonstrate a generic struct
using System;

struct XY<T> {
  T x;
  T y;

  public XY(T a, T b) {
    x = a;
    y = b;
  }

  public T X {
    get { return x; }
    set { x = value; }
  }

  public T Y {
    get { return y; }
    set { y = value; }
  }

}

class StructTest {
  public static void Main() {
    XY<int> xy = new XY<int>(1, 2);
    XY<double> xy2 = new XY<double>(8.0, 9.0);
    Console.WriteLine(xy.X + ", " + xy.Y);

    Console.WriteLine(xy2.X + ", " + xy2.Y);
  }
}

           
       








Related examples in the same category