Compiler Error CS0417
'identifier': cannot provide arguments when creating an instance of a variable type
This error occurs if a call to the new operator on a type parameter has arguments. The only constructor that can be called using the new operator on an unknown parameter type is a constructor with no arguments. If you need to call another constructor, consider using a class type constraint or interface constraint.
Constructor constraint only works with default constructor
You cannot call other constructors than the default constructor. Something like
class TBase
{
private int ii;
public TBase(int i)
{
ii = i;
}
}
class C<T> where T : TBase
{
T type = new T(42);
}
doesn't work!
CS Team edit: Correct. Your version will cause CS0304. Then, if you change the declaration of C to include the new constraint, you will get this error.
class C<T> where T : TBase, new()
{
T type = new T(42);
}
- 2/18/2008
- Griestopf
- 5/5/2010
- SJ at MSFT