Sunday, April 22, 2007 2:11 PM
kolbis
To sealed or not to sealed?
Sealed is pretty powerful modifier in .Net. However I don't think that everyone understands the true essence of it. I want to emphasize the meaning of the sealed modifier.
The sealed modifier can be applied to classes, instance methods and properties.
The sealed modifier in a class declaration is used to prevent inheritance of the class:
sealed class SealedClass
{
public int x;
public int y;
}
A sealed method overrides a method in a base class, but itself cannot be overridden further in any derived class.
The main purpose of a sealed class to take away the inheritance feature from the user. Here are some other points to think about:
If the sealed class is inheriting another class, using the sealed
keyword either in the method or class declaration make the CLR convert all
calls from virtual to normal method calls, because the runtime knows that
this method and can't be overridden. This can improve performance.
If the class has only static fields and properties, it doesn't make
sense to inherit the class. So the class designer can choose to seal his
class to avoid confusion.
תגים:CLR, .NET, .NET Framework