This C# keyword is used on a class
—it means the class
cannot be derived from. Besides its conceptual meaning, sealed may impact performance.
This keyword prevents derivation, and it sometimes has a positive impact on performance. Further testing is necessary. The optimization is sometimes not applied.
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.
interface
with one method (ITest
) and 2 classes that implement that interface
.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
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'
We find evidence (in 2021) of the small, but measurable, speedup from using sealed. The sealed keyword improves performance on repeated method invocations.
class
that is not sealed. The GetNumber
method is repeatedly called.class
that has the sealed keyword. It performs the same actions as version 1.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
What mechanism does .NET use to call interface
(or virtual
) methods? Languages that implement class
-level polymorphism store a virtual
method dispatch table.
virtual
method dispatch.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.