C# compiler requires new() constraint to be specified last.
According to MSDN:
When used together with other
constraints, the new() constraint must
be specified last.
Why is there such restriction?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because the specification says so. It probably says so because it makes parsing the constraints a little easier. There is little value in allowing you to specify the constraints in any order and I can imagine some cost (including opportunity cost!) in making it possible.
Note, in fact, that the specification doesn’t just say that you have to have the constructor constraint last, if you have it. It actually says that you have to have the constraints in the following order
Where a primary constraint is one that specifies the type parameter must be a reference type or a value type, secondary constraints are ones that specify a base class or interfaces, and the constructor constraint is the one under discussion here.
The relevant section of the specification is §10.1.5 and is definitely worthwhile reading.