T
tbandrow
I would like to get this to compile:
public class GenericCtor<T> where T: class, new()
{
public static T MakeIt()
{
return new T(1, 2);
}
}
public class GenericCtorItem
{
int A, B;
public GenericCtorItem(int a, int b)
{
A = a;
B = b;
}
}
The documentation says: "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."
Which to me, implies that if use a class type or interface constraint,
this should work:
public class GenericCtor<T> where T: GenericCtorItem, new()
{
public static T MakeIt()
{
return new T(1, 2);
}
}
public class GenericCtorItem
{
int A, B;
public GenericCtorItem(int a, int b)
{
A = a;
B = b;
}
}
But, it doesn't. Is there a way at all to call a constructor for this?
public class GenericCtor<T> where T: class, new()
{
public static T MakeIt()
{
return new T(1, 2);
}
}
public class GenericCtorItem
{
int A, B;
public GenericCtorItem(int a, int b)
{
A = a;
B = b;
}
}
The documentation says: "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."
Which to me, implies that if use a class type or interface constraint,
this should work:
public class GenericCtor<T> where T: GenericCtorItem, new()
{
public static T MakeIt()
{
return new T(1, 2);
}
}
public class GenericCtorItem
{
int A, B;
public GenericCtorItem(int a, int b)
{
A = a;
B = b;
}
}
But, it doesn't. Is there a way at all to call a constructor for this?