Pro EP 75 : When to make our classes/records sealed?

Muhammad Waseem
Become .NET Pro !
1 min readOct 6, 2023

--

In .NET, you can use the sealed modifier for classes and records to restrict them from being inherited by other classes or records.

In following situations we should use it:

1) When you are sure that this implementation is complete and no one is going to inherit it. For example Handler from CQRS perspectives

2) When you are concerned about immutability or security you should seal it.

3) While working on mini performance spots and their are few classes that are not being inherited by anyone.

If you are sure that no one will inherit them then seal it because a sealed class/record is more performant then
un-sealed.

Is there any another situation in which we should make our class sealed ?

Whenever you’re ready, there are 4 ways I can help you

  1. Subscribe to my Weekly .NET Newsletter of C#/.NET with b6000+ Software Engineers.
  2. Promote yourself to 6000+ subscribers by Sponsoring my Newsletter
  3. Download my eBook at Gum Road which contains 30+ .NET Tips (With 2800+ Downloads)
  4. To get 100+ free and paid resources for learning C# and .NET visit this GitHub Repository

--

--