This topic has not yet been rated - Rate this topic

Compiler Error CS0310

The type 'typename' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic'

The generic type or method defines a new constraint in its where clause, so any type must have a public parameterless constructor in order to be used as a type argument for that generic type or method. To avoid this error, make sure that the type has the correct constructor, or modify the constraint clause of the generic type or method.

The following sample generates CS0310:

// CS0310.cs
using System;

class G<T> where T : new()
{
    T t;

    public G()
    {
        t = new T();
        Console.WriteLine(t);
    }
}

class B
{
    private B() { }
    // Try this instead:
    // public B() { }
}

class CMain
{
    public static void Main()
    {
        G<B> g = new G<B>();   // CS0310
        Console.WriteLine(g.ToString());
    }
}
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Clarification
To clarify, I got this message when the class being defined did not have a 'new()' constraint on it and the T being passed was non-abstract and defined a public parameterless constructor. I added the 'new()' constraint and the error went away.
This error message is misleading
I got this message when a function lacked the constraint specification of such. The T that I passed would have satisfied the constraint if it were present and thus the confusion.

Maybe a 'constraint missing...' message would be more helpful??