Csharp/CSharp Tutorial/struct/Generic struct
A generic struct
<source lang="csharp">using System;
struct GenStruct<T> {
T x; T y; public GenStruct(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 MainClass {
public static void Main() { GenStruct<int> xy = new GenStruct<int>(10, 20); GenStruct<double> xy2 = new GenStruct<double>(88.0, 99.0); Console.WriteLine(xy.X + ", " + xy.Y); Console.WriteLine(xy2.X + ", " + xy2.Y); }
}</source>
10, 20 88, 99
Generic Struct
<source lang="csharp">using System; using System.Collections.Generic; struct GenericStruct<T> {
private T _Data; public GenericStruct(T value) { _Data = value; } public T Data { get { return _Data; } set { _Data = value; } }
} class MainClass {
static void Main() { GenericStruct<int> IntData = new GenericStruct<int>(10); GenericStruct<string> StringData = new GenericStruct<string>("str"); Console.WriteLine("IntData = {0}", IntData.Data); Console.WriteLine("StringData = {0}", StringData.Data); }
}</source>
IntData = 10 StringData = str