Can you guess what is the reason to not allow sealed classes for type-constraints in generics? I only have one explanation is to give opportunity to use naked constraints.
Can you guess what is the reason to not allow sealed classes for type-constraints
Share
If the class is sealed it cannot be inherited. If it cannot be inherited it’d be the only type valid for the generic type argument [assuming if allowed to be a type argument]. If it is the only generic type argument then there’s no point in making it generic! You can simply code against the type in non-generic class.
Here’s some code for this.
In addition to this, You can also not have a static class as generic type-constraint. The reason is simple. Static classes are marked as abstract and sealed in compiled IL which can be neither instantiated nor inherited.
Here’s the code for this.