Generic Structures


C# allows you to create generic structures. The syntax is the same as for generic classes. For example, in the following program, the XY structure, which stores X, Y coordinates, is generic:

 // Demonstrate a generic struct. using System; // This structure is generic. 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>(10, 20);     XY<double> xy2 = new XY<double>(88.0, 99.0);     Console.WriteLine(xy.X + ", " + xy.Y);     Console.WriteLine(xy2.X + ", " + xy2.Y);   } }

The output is shown here:

 10, 20 88, 99

Like generic classes, generic structures can have constraints. For example, this version of XY restricts type arguments to value types:

 struct XY<T> where T : struct { // ...




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net