Demonstrate a new() constructor constraint. : New constaint : Generic C# Examples


C# Examples » Generic » New constaint »

 

Demonstrate a new() constructor constraint.









    
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>();  
  
    }  
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Generic
» New constaint