I Have following problem:
class Request<T>
{
private T sw;
public Request()
{
//How can i create here the instance like
sw = new T();
}
}
is it possible to do it?
Add a new constraint:
class Request<T> where T : new() {
private T sw;
public void Request() {
sw = new T();
}
}
This tells the compiler that T will always have an accessible parameterless constructor (no, you can't specify any other kind of constructor).
You need to declare the constraint where T : new() in the class declaration. This is restricting T to types with a public default constructor. For example:
class Request<T> where T : new()
{
private T sw;
public Request()
{
sw = new T();
}
}
More information: http://msdn.microsoft.com/en-us/library/d5x73970.aspx
You need to tell the compiler that T always implements a parameterless constructor.
class Request<T> where T : new()