The Significant Usage of Sealed Class in .NET Development
Intro
Hi guys, welcome to my another Performance series specially for .NET Code and how those code really impact the performance in our daily development tasks.
Click Collection Perfomance Series for full performance benchmark.
What is a Sealed Class?
A sealed class in .NET is a class that cannot be inherited from. This means once a class is marked as sealed
, no other class can derive from it. This is done by using the sealed keyword in the class definition. But this class marked as sealed can inherit from another class. For example:
public sealed class SealTypeClass : BaseClass
{
public override string GetStringValue()
{
return "this is class with Seal";
}
}
Cannot be inherited which will receive compilation error.
Why Use Sealed Classes?
Sealed classes serve multiple purposes in .NET development:
- Performance Optimization: By preventing inheritance, the .NET runtime can optimize method calls and access, potentially improving performance. This is because the runtime knows that certain methods are not subject to virtual dispatch.
- Design Intent: Sealed classes can be used to explicitly indicate that a class is designed to be used as-is and not to be extended. This can be particularly useful for API designers who want to prevent modification of core functionality.
- Security: By sealing a class, you prevent others from altering its behavior through inheritance. This can be crucial for maintaining security in sensitive parts of your application.
Let`s do Performance Benchmark 🚀
The fun part where we start coding 😊. First, i made a base class which will have a function GetStringValue
whch return a string.
public class BaseClass
{
public virtual string GetStringValue()
{
return "this is from base class";
}
}
Second, make a non sealed class called NonSealTypClass
which inherit from BaseClass
:
public class NonSealTypClass : BaseClass
{
public override string GetStringValue()
{
return "this is non seal class";
}
}
Next, class with sealed called SealTypeClass
and also inherits from BaseClass
:
public sealed class SealTypeClass : BaseClass
{
public override string GetStringValue()
{
return "this is class with Seal";
}
}
Now, we start make a new class name BenchMarkSealClass
which will handle the benchmark by using BenchmarkDotNet.
Settings I added
[MemoryDiagnoser]
to see the momory allocated
[SimpleJob(runtimeMoniker:RuntimeMoniker.Net80)]
for specific .NET Runtime which is .NET8
[SimpleJob(runtimeMoniker:RuntimeMoniker.Net80)]
[MemoryDiagnoser]
public class BenchMarkSealClass
{
private NonSealTypClass _nonSealTypClass;
private SealTypeClass _sealTypeClass;
[GlobalSetup]
public void Setup()
{
_nonSealTypClass = new NonSealTypClass();
_sealTypeClass = new SealTypeClass();
}
[Benchmark]
public void NonSeal()
{
_nonSealTypClass.GetStringValue();
}
[Benchmark]
public void WithSeal()
{
_sealTypeClass.GetStringValue();
}
}
Results
- WithSeal :
0.2329ns
- NonSeal :
0.2730ns
As per below result, we can see the sealed
class can improve ~15% of performance compare to non sealed class. Nice weh! 🚀
Summary
In summary, a sealed class is a way to finalize the design of a class and prevent any further subclassing, which can help with security and design integrity. And the significant performance boost in your application 🚀.