#535 – Creating a Generic Struct
March 8, 2012 Leave a comment
In addition to generic classes, you can also create a generic struct. Like a class, the generic struct definition serves as a sort of template for a strongly-typed struct. When you declare a variable of this struct type, you provide a type for its generic parameter.
public struct ThreeTuple<T> { public ThreeTuple(T x, T y, T z) { X = x; Y = y; Z = z; } public T X; public T Y; public T Z; } public class Program { static void Main() { ThreeTuple<int> intTuple = new ThreeTuple<int>(32, 10, 12); int yVal = intTuple.Y; ThreeTuple<double> dblTuple = new ThreeTuple<double>(1.2, 3.4, 5.6); double yVal2 = dblTuple.Y; } }