A value type constriant : Type constaint « Generic « C# / CSharp Tutorial






using System; 
 
struct MyStruct { 
} 
 
class MyClass { 
} 
 
class Test<T> where T : struct { 
  T obj; 
 
  public Test(T x) { 
    obj = x; 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    Test<MyStruct> x = new Test<MyStruct>(new MyStruct()); 
    Test<int> y = new Test<int>(10); 
 
    // illegal! 
    // Test<MyClass> z = new Test<MyClass>(new MyClass()); 
  } 
}








18.21.Type constaint
18.21.1.A reference constriant
18.21.2.A value type constriant
18.21.3.Static Field Per Closed Type