Is it pointless to have both "class" and "new()" constraints in a generic class?
c#, constraints, generics
Solution
`new()` doesn't imply a reference type, so: No, `class` is not redundant when using `new()`.
The following code demonstrates that:
void Main()
{
new MyParanoidClass<S>();
}
struct S {}
class MyParanoidClass<T> where T : new()
{
//content
}
This code compiles, proving that `new()` doesn't care if you use a reference or a value type.
Problem
I am wondering if it makes any sense to have both "class" and "new()" constraints when defining a generic class. As in the following example: ``` class MyParanoidClass<T> where T : class, new() { //content } ``` Both constraints specify that T should be a reference type. While the "class" constraint does not imply that a implicit constructor exists, the "new()" constraint does require a "class" with an additional public constructor definition. My final (formulation for the) question is: do I have any benefits from defining a generic class as in the above statement, or does "new()" encapsulate both constraints?