When defining a generic type parameter\'s constraints, we have to put class()
at the front and new()
at the end, for example.
Why is this,
Like most syntax related questions, the basic answer is because the spec says so. We get the following grammar for generic type constraints from the C# 5.0 spec (Section 10.1.5)
type-parameter-constraints:
primary-constraint secondary-constraints constructor-constraint primary-constraint , secondary-constraints primary-constraint , constructor-constraint secondary-constraints , constructor-constraint primary-constraint , secondary-constraints , constructor-constraint
primary-constraint:
class-type class struct
secondary-constraints:
interface-type type-parameter secondary-constraints , interface-type secondary-constraints , type-parameter
constructor-constraint:
new ( )
Eric Lippert has done an excellent job of explaining why it was designed this way, so I won't expound on that.