Extend Sealed Class in C# Using Extension Method - A Simple Approach

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.

  1. public sealed class DotnetPiper  
  2. {  
  3.     public DotnetPiper()  
  4.     {  
  5.         Console.WriteLine("D Class Constructor called!");  
  6.     }  
  7.    public void DotnetPiperInstanceMethod()  
  8.     {  
  9.         Console.WriteLine("DotnetPiper is Called!");  
  10.     }  
  11. }  
  12. public class DotnetPiperExtension : DotnetPiper  
  13. {  
  14.   
  15. }  
As soon as we build our solution we will get the following error:

Error 1 'OOPS_App.DotnetPiperExtension': cannot derive from sealed type,

Erro

I have created an extension class that keeps a method to extend the functionality of the sealed class.
  1. public static class DotnetPiperExtension  
  2. {  
  3.     public static string DotnetPiperExtentionMethod(this DotnetPiper objDotnet, string str)  
  4.     {  
  5.         return "Welcome to the World of DotNet....Mr. " + str;  
  6.     }  
  7. }  
Please have a look at the following image:

method

Here I am calling an extension method in the main class as in the following:
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         DotnetPiper dp = new DotnetPiper();  
  6.         string nameStr = dp.DotnetPiperExtentionMethod("Sachin Kalia");  
  7.         Console.WriteLine(nameStr);  
  8.         Console.ReadLine();  
  9.     }  
  10. }  
Output

CMD

The complete code for hands-on:
  1. public sealed class DotnetPiper  
  2. {  
  3.     public DotnetPiper()  
  4.     {  
  5.         Console.WriteLine("D Class Constructor called!");  
  6.     }  
  7.    public void DotnetPiperInstanceMethod()  
  8.     {  
  9.         Console.WriteLine("DotnetPiper is Called!");  
  10.     }  
  11. }  
  12.   
  13.   
  14. public static class DotnetPiperExtension  
  15. {  
  16.     public static string DotnetPiperExtentionMethod(this DotnetPiper objDotnet, string str)  
  17.     {  
  18.         return "Welcome to the World of DotNet....Mr. " + str;  
  19.     }  
  20. }  
  21.   
  22.   
  23. class Program  
  24. {  
  25.     static void Main(string[] args)  
  26.     {  
  27.         DotnetPiper dp = new DotnetPiper();  
  28.         string nameStr = dp.DotnetPiperExtentionMethod("Sachin Kalia");  
  29.         Console.WriteLine(nameStr);  
  30.         Console.ReadLine();  
  31.   
  32.                 }  
  33.        }  
Note: It might be one of the FAQs in an interview.


Recommended Free Ebook
Similar Articles