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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| 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; } } |