C# sealed Keyword: Cannot Derive From Sealed Type

Use the sealed keyword to prevents class derivation. Measure the performance impact of sealed.
Sealed. This keyword is used on a class. It means the class cannot be derived from. Besides its conceptual meaning, sealed may impact performance.KeywordsClass
This keyword prevents derivation. And it sometimes has a positive impact on performance. Further testing is necessary. The optimization is sometimes not applied.
An example. Applying the sealed keyword tells the C# compiler to apply the "sealed" metadata decoration in the assembly of your class. The sealed keyword is a syntax hint.

Tip: The JIT compiler can also detect the sealed decoration and optimize method invocations better when it is used.

Next: We show an interface with one method (ITest) and 2 classes that implement that interface.

Here: The first class, TestA, is not sealed. The second class, TestB, has the sealed decoration.

C# program that uses sealed class using System; /// <summary> /// Example interface. /// </summary> interface ITest { /// <summary> /// Method required by the interface. /// </summary> int GetNumber(); } /// <summary> /// Non-sealed class that implements an interface. /// </summary> class TestA : ITest { /// <summary> /// Interface implementation. /// </summary> public int GetNumber() { return 1; } } /// <summary> /// Sealed class that implements an interface. /// </summary> sealed class TestB : ITest { /// <summary> /// Interface implementation. /// </summary> public int GetNumber() { return 2; } } class Program { static void Main() { ITest test1 = new TestA(); // Regular class ITest test2 = new TestB(); // Sealed instantiation Console.WriteLine(test1.GetNumber()); // TestA.GetNumber Console.WriteLine(test2.GetNumber()); // TestB.GetNumber } } Output 1 2
A discussion. What mechanism does the .NET Framework use to call interface (or virtual) methods? Languages that implement class-level polymorphism store a virtual method dispatch table.Interface

Then: The runtime looks for the method location during execution. Each type has a Type pointer. This is used for virtual method dispatch.


Also: If you execute this program in the Visual Studio environment, the sealed optimization is not applied.

However: You can run the program outside of the debugger by clicking on the executable.

Benchmark. We find evidence of the small but measurable speedup allowed by sealed. The sealed keyword can improve performance by about a third of a nanosecond per method invocation.

Tip: For interface-heavy programs, the sealed decoration can result in a speedup on all method calls with no downside.

Info: Five method calls in inner loops were done together. Results take this into account and divide by 5.

Outer variables used in benchmark int sum1 = 0; int sum2 = 0; ITest test1 = new TestA(); ITest test2 = new TestB(); Contents of tight loops sum1 += test1.GetNumber(); // Loop 1 body sum1 += test1.GetNumber(); sum1 += test1.GetNumber(); sum1 += test1.GetNumber(); sum1 += test1.GetNumber(); sum2 += test2.GetNumber(); // Loop 2 body sum2 += test2.GetNumber(); sum2 += test2.GetNumber(); sum2 += test2.GetNumber(); sum2 += test2.GetNumber(); Output Iterations: 1000000 * 100 TestA.GetNumber (regular): 2.490 ns TestB.GetNumber (sealed): 2.162 ns [faster]
An error. Here we try to derive from a sealed class. Sometimes, writing programs that are incorrect can teach us more about how the language works.
C# program that causes sealed error class Bird { } sealed class Test : Bird { } class Example : Test { } class Program { static void Main() { } } Output Error CS0509 'Example': cannot derive from sealed type 'Test'
A summary. We applied sealed to a class. It produced a measurable speedup on all method invocations. Sealed has performance benefits—these depend on the execution environment.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls