Here we will extend a sealed class in C# using an extension method.
Requirement: What is sealed. Can we derive this? If yes, then how we can do this?
Friends, this is one of the possible situations that you may encounter in your daily needs or maybe in an interview. Let's dive into it in a practical way.
Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as a sealed class, the class cannot be inherited.
In C#, the sealed modifier is used to define a class as sealed. In Visual Basic .NET the NotInheritable keyword serves the purpose of sealed. If a class is derived from a sealed class then the compiler throws an error.
Sealed Methods and Properties
You can also use the sealed modifier on a method or a property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent other developers that are using your classes from overriding specific virtual methods and properties.
Here we are not directly extending the functionality using the inheritance feature. We can do this using an extension method. The following is the code declaration with practical hands-on.
Note: You should have knowledge of extension method to implement this.
- public sealed class DotnetPiper
- {
- public DotnetPiper()
- {
- Console.WriteLine("D Class Constructor called!");
- }
- public void DotnetPiperInstanceMethod()
- {
- Console.WriteLine("DotnetPiper is Called!");
- }
- }
- public class DotnetPiperExtension : DotnetPiper
- {
-
- }
As soon as we build our solution we will get the following error:
Error 1 'OOPS_App.DotnetPiperExtension': cannot derive from sealed type,
I have created an extension class that keeps a method to extend the functionality of the sealed class.
- public static class DotnetPiperExtension
- {
- public static string DotnetPiperExtentionMethod(this DotnetPiper objDotnet, string str)
- {
- return "Welcome to the World of DotNet....Mr. " + str;
- }
- }
Please have a look at the following image:
Here I am calling an extension method in the main class as in the following:
- class Program
- {
- static void Main(string[] args)
- {
- DotnetPiper dp = new DotnetPiper();
- string nameStr = dp.DotnetPiperExtentionMethod("Sachin Kalia");
- Console.WriteLine(nameStr);
- Console.ReadLine();
- }
- }
OutputThe complete code for hands-on:
- public sealed class DotnetPiper
- {
- public DotnetPiper()
- {
- Console.WriteLine("D Class Constructor called!");
- }
- public void DotnetPiperInstanceMethod()
- {
- Console.WriteLine("DotnetPiper is Called!");
- }
- }
-
-
- public static class DotnetPiperExtension
- {
- public static string DotnetPiperExtentionMethod(this DotnetPiper objDotnet, string str)
- {
- return "Welcome to the World of DotNet....Mr. " + str;
- }
- }
-
-
- class Program
- {
- static void Main(string[] args)
- {
- DotnetPiper dp = new DotnetPiper();
- string nameStr = dp.DotnetPiperExtentionMethod("Sachin Kalia");
- Console.WriteLine(nameStr);
- Console.ReadLine();
-
- }
- }
Note: It might be one of the FAQs in an interview.