I am trying to test a method - and getting an error:
Cannot create an instance of the variable type \'Item\' because it does not have the new() constr
Because many people get here by the question tilte (which is very generic and matches the compiler message) let me give a more detailed answer about the compiling error itsef.
You are using generics in a method. The compiler doesn´t know which type it will receive and thus it is not warranted that your type has a parameterless construtor. For ex:
class A {
A(int i){ ... }
}
class B { ... }
public void MyMethod(){
T t = new T(); //This would be fine if you use 'MyMethod' but you would have a problem calling 'MyMethod' (because A doesn´t have a parameterless construtor;
}
To resolve this, you can tell the compiler that your generic parameter has a parameterless construtor. This is done by defining constraints:
public void MyMethod() where T: new(){
T t = new T(); //Now it's ok because compiler will ensure that you only call generic method using a type with parameterless construtor;
}
More information about constructor constraints may be found here: https://msdn.microsoft.com/en-us/library/bb384067.aspx