Sunday, July 25, 2010

Sealed Class

Sealed Class is the class which can not be inherited.the members/methods in the sealed class can not be derived from any other class. A compile-time error occurs if a sealed class is specified as the base class of another class. By default, structs are sealed; not classes in .NET

Here we see evidence of the small but measurable speedup allowed by the use of the keyword sealed in the C# programming language. The benchmark shows that for the very simplest interface methods, the sealed keyword can improve performance by about a third of a nanosecond per method invocation. For interface-heavy programs, the sealed keyword decoration can result in a speedup on all method calls with no downside.

~~~ Sealed class benchmark results (C#) ~~~       
See "Benchmark" section for the code compared.

TestA.GetNumber (regular): 2.490 ns
TestB.GetNumber (sealed): 2.162 ns [faster]

So, the keyword sealed provides a way to demand that the class not to be inherited from, but it is also useful for performance optimization.

No comments:

Post a Comment