Possible Duplicate:
What does new() mean?
Like in title. I wonder what this syntax in the code means. I’ve find it in some samples but it wasn’t explained and I don’t really know what it does.
public class SomeClass<T> where T: new() // what does it mean?
Can anyone explain that for me?
Perhaps you mean you saw something along these lines?
which means that you can only use the generic class with a type T that has a public parameterless constructor. These are called generic type constraints. I.e., you cannot do this (see CS0310):
Why would you need such constraint? Suppose you want to instantiate the a new variable of type
T. You can only do that when you have this constraint, otherwise, the compiler cannot know beforehand whether the instantiation works. I.e.: