A value type constriant : Type constaint : Generic C# Examples


C# Examples » Generic » Type constaint »

 

A value type constriant









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




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Generic
» Type constaint