We make heavy use of generics in our projects. Generics are really great deal when it comes to run-time polymorphism. Recently, one of my colleague came up with an idea to use a sealed class as generic type constraint. He wrote a sample code that looks like this.
public sealed class MySealedClass
{
public MySealedClass() { }
}
public class MyGeneric<T>
where T : MySealedClass
{
public MyGeneric() { }
}
Surprisingly, Above code gives following compile error.
That was strange. We never observed such behavior before. Code was absolutely correct and it seemed like language designers had intentionally put this constraint to not allow sealed classed as generic constraints. But what must be the reason behind this ? Finally we had our thoughts in place and found the fact why it is so.
To understand, why sealed classes are not allowed to be generic type constraints, let us assume that sealed classes are allowed to be type constraints to generics. With this assumption the code above is valid and compiles successfully. Now lets write some client-code that would use this generic.
//create and instantiate an object of type MyGeneric MyGeneric<MySealedClass> objGeneric = new MyGeneric<MySealedClass>();
Fair enough, this looks ok.
Now lets check what other types we can pass to MyGeneric type-argument at runtime. By defination, it has to be MySealedClass itself or any type that inherits from MySealedClass.
But won’t MySealedClass be the only valid type-argument for MyGeneric since MySealedClass cannot be inherited further and there will be no class that inherits from MySealedClass ? Doesn’t this defeat the whole idea of putting MySealedClass in type constraint to MyGeneric, since there’ll be no other valid-type argument except MySealedClass itself ? If MySealedClass is going to be the only valid type for MyGeneric, then there’s no point in defining MyGeneric as generic at all. Can’t we simply code against the type MySealedClass ?
We could finally realize the reason why language designers of C# didn’t allow sealed classes as generic type-argument. quite enlightening.
While doing this we also came to realize why static classes are also not allowed to be generic type-constraints, though it’s very much obvious that static classes can never ever be generic type constraints. We found our answer in the compiled IL, where the static classes are marked as both abstract and sealed. Abstract since they cannot be instantiated and sealed since they cannot be inherited, hence cannot be generic type-constraints. In general only those types can be generic type-constraints which can have inheritance hierarchy below them, this includes interfaces, any non-sealed abstract or concrete classes.

Interestring ………….. Awsome………….
Comment by pranay rana on July 7, 2010 at 10:37 PM
Indeed interesting post. Keep it up ruchit.
Comment by Krunal on July 8, 2010 at 10:21 AM
good work ruchit, really awsome topic you have explained. Thanks
Comment by Jignesh on July 8, 2010 at 8:52 PM
Excellent!!! Great work!!! keep it up.
Comment by Aditya Jaiswal on July 9, 2010 at 4:14 PM
Thanks for your comments folks.
Comment by Ruchit on July 14, 2010 at 9:00 PM
Very welll written!
Comment by Jay Joshi on July 15, 2010 at 9:10 PM