Sealed Keyword in .NET C# Every Developer must Know!



Sealed Keyword in .NET C# Every Developer must Know!

Sealed Keyword in .NET C# Every Developer must Know!

#coding #codingbootcamp #softwaredeveloper

https://StartupHakk.com/?v=LNY2006pIh8

GitHub Repo: https://github.com/slthomason/StartupHakk/tree/main/55_Sealed_Keyword_In_CSharp

Any .NET code passes by more than one phase till finally reaches the machine code. Since many factors are involved in this process, there could be lots of details that we miss when we are first writing our code.

However, the more clear and deterministic the code we write is, the more the compiler could assist us and generate optimized machine code.

In this video, we are going to discuss one example of the ways by which we can help the compiler optimize our code. This way is; using the Sealed Keyword.

If you are a .NET developer, even a beginner, you should know by now that there is a keyword in the .NET framework called sealed.

This keyword could be used in a class definition and then it means that the class could not be inherited by any other classes.

Or even in a method declaration and then it means that the method could not be overridden -anymore- by any other method in a child class. In other words, it breaks the method override series at the level where it is used.

Therefore, what we can understand from this is that when we use the sealed keyword, we are actually promising the compiler that we don’t have any intentions to inherit from a class or override a method.

Calling Virtual Method
To validate if there would be any difference -from the compiler’s point of view- between calling a virtual method on both MyClass and MySealedClass classes. The performance of calling the virtual method on the sealed class is much better than calling it on the non-sealed class.

Calling Non-Virtual Method
To validate if there would be any difference -from the compiler’s point of view- between calling a non-virtual method on both MyClass and MySealedClass classes. The performance of calling the non-virtual method on the sealed class is better than calling it on the non-sealed class.

Type Checking
To validate if there would be any difference -from the compiler’s point of view- between checking the type of an object using the is operator on both MyClass and MySealedClass classes. The performance of checking the object type on the sealed class is better than calling it on the non-sealed class.

Type Casting
To validate if there would be any difference -from the compiler’s point of view- between casting an object using the as operator on both MyClass and MySealedClass classes. The performance of casting the object on the sealed class is better than calling it on the non-sealed class.

Storing Object In Array
To validate if there would be any difference -from the compiler’s point of view- between storing an object in an array on both MyClass and MySealedClass classes. The performance of storing an object in an array on the sealed class is better than calling it on the non-sealed class.

Early Failure Detection
In addition to the performance gain we can get by using the sealed keyword, we can also avoid some runtime failures. The compiler -at design time- would not show any warnings or errors because actually obj could be of type MyClass class or any of its child classes. Therefore, the compiler needs to wait for the runtime to do the final check.

For sure, if at runtime the real type of obj turns out to not implement IMyInterface, this would cause a runtime exception.

The compiler would show an error (CS0039) in the design time because obj could only be of type MySealedClass class, nothing else. Therefore, the compiler can instantly check if MySealedClass class implements IMyInterface or not.

Thus, this means that using the sealed keyword enabled the compiler to perform the proper static changes at design time.

Final Thoughts
I would always recommend using the sealed keyword whenever applicable.

This is not only for the performance gain you might get but also because it is a best practice from the design point of view to the extent that Microsoft was actually thinking about making all classes sealed by default.

Comments are closed.