Demonstrate a new() constructor constraint. : new constaint « Generic « C# / CSharp Tutorial






using System; 
 
class MyClass { 
 
  public MyClass() { 
  }     
} 
 
class Test<T> where T : new() { 
  T obj; 
 
  public Test() { 
    obj = new T(); // create a T object 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    Test<MyClass> x = new Test<MyClass>(); 
 
  } 
}








18.20.new constaint
18.20.1.Demonstrate a new() constructor constraint.