I have noticed someone has done this in C# - notice the new()
public class MyClass<T> where T: new(){
//etc
}
What does this achieve?
This constrains the generic MyClass<T>
to only work with T
instances that have an available parameterless constructor. This allows you to safely use the following expression within the type
new T()
Without the new
constraint this would not be allowed because the CLR couldn't verify the type T
had an applicable constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With