Home
Map
sealed KeywordUse the sealed keyword to prevents class derivation. Measure the performance impact of sealed.
C#
This page was last reviewed on Jun 26, 2021.
Sealed. This C# keyword is used on a class. It means the class cannot be derived from. Besides its conceptual meaning, sealed may impact performance.
class
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.
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 } }
1 2
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.
class Bird { } sealed class Test : Bird { } class Example : Test { } class Program { static void Main() { } }
Error CS0509 'Example': cannot derive from sealed type 'Test'
Sealed benchmark. We find evidence (in 2021) of the small, but measurable, speedup from using sealed. The sealed keyword improves performance on repeated method invocations.
Stopwatch
Benchmark
Version 1 This version of the code calls methods on a derived class that is not sealed. The GetNumber method is repeatedly called.
Version 2 Here we call a method on a derived class that has the sealed keyword. It performs the same actions as version 1.
Result For interface-heavy programs, the sealed decoration can result in a speedup on all method calls with no downside.
using System; using System.Diagnostics; interface ITest { int GetNumber(); } class TestA : ITest { public int GetNumber() { return 1; } } sealed class TestB : ITest { public int GetNumber() { return 2; } } class Program { const int _max = 10000000; static void Main() { int sum1 = 0; int sum2 = 0; ITest test1 = new TestA(); ITest test2 = new TestB(); var s1 = Stopwatch.StartNew(); // Version 1: use non-sealed class method calls. for (int i = 0; i < _max; i++) { sum1 += test1.GetNumber(); sum1 += test1.GetNumber(); sum1 += test1.GetNumber(); sum1 += test1.GetNumber(); sum1 += test1.GetNumber(); } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use sealed class method calls. for (int i = 0; i < _max; i++) { sum2 += test2.GetNumber(); sum2 += test2.GetNumber(); sum2 += test2.GetNumber(); sum2 += test2.GetNumber(); sum2 += test2.GetNumber(); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } }
2.31 ns Not sealed class, GetNumber 1.86 ns Sealed class, GetNumber
A discussion. What mechanism does .NET 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.
Type
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.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 26, 2021 (edit).
Home
Changes
© 2007-2024 Sam Allen.