A new() constructor constraint : Generic Constraint : Generics C# Source Code


Custom Search

C# Source Code » Generics » Generic Constraint »

 

A new() constructor constraint








    


using System;

class MyClass {

  public MyClass() {
  }

}

class Test<T> where T : new() {
  T obj;

  public Test() {
    // This works because of the new() constraint.
    obj = new T(); // create a T object
  }
}

class ConsConstraintDemo {
  public static void Main() {

    Test<MyClass> x = new Test<MyClass>();

  }
}
           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Generics
» Generic Constraint