Compiler Error CS0315

The type 'valueType' cannot be used as type parameter 'T' in the generic type or method 'TypeorMethod<T>'. There is no boxing conversion from 'valueType' to 'referenceType'.

This error occurs when you constrain a generic type to a particular class, and try to construct an instance of that class by using a value type that cannot be implicitly boxed to it.

To correct this error

  1. One solution is to redefine the struct as a class.


The following example generates CS0315:

// cs0315.cs  
public class ClassConstraint { }  
public struct ViolateClassConstraint { }  
public class Gen<T> where T : ClassConstraint  
public class Test  
    public static int Main()  
        Gen<ViolateClassConstraint> g = new Gen<ViolateClassConstraint>(); //CS0315  
        return 1;  

See also