DCSIMG

 Subscribe in a reader

To sealed or not to sealed? - Guy kolbis
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.
תגים:, ,

תוכן התגובה

# re: To sealed or not to sealed?

arnonrgo כתב/ה

Usually I wouldn't recommend using the sealed keyword

if you have performance problems it is better to find the problem with your design/architecture rather then try to solve it this way

Also sealing a class violates the Open Closed Principle (see http://www.rgoarchitects.com/Files/ooprimer.ppt)

if the class has only static methods make the class static (added in .Net 2.0..) which makes it sealed anyway

Sunday, April 22, 2007 3:19 PM

# re: To sealed or not to sealed?

kolbis כתב/ה

It is not frequently that I use sealed keyword on my classes; however this is definitely something that an average programmer must know.

Regarding the static keyword, you can find similarities between the two, but one must not confuse them. Think of a scenario where the sealed class inherits from a base class. If so you cannot use the static keyword.

Sunday, April 22, 2007 3:39 PM

# re: To sealed or not to sealed?

Sasha Goldshtein כתב/ה

Despite what everyone says, you can just look at the JIT-generated code and see for yourself that the "sealed" modifier on a class doesn't actually change the code generation.  The code emitted is still a virtual method call.

Which kind of invalidates the performance aspect, and only leaves the design and security aspects (the latter you didn't mention, but it's also important; I wouldn't want anyone deriving from String and giving me a different implementation, especially if all methods were virtual by default like Java's).

Anyway, you can read more at my blog if you'd like:

blogs.microsoft.co.il/.../JIT-Optimizations_2C00_-Inlining_2C00_-and-Interface-Method-Dispatching-_2800_Part-1-of-N_2900_.aspx

Monday, December 17, 2007 11:13 PM

שלח תגובה

(שדה חובה) 
(שדה חובה) 
(אופציונלי)
(שדה חובה) 

Enter the numbers above: