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
- {
- }
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;
- }
- }

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();
- }
- }

The 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();
- }
- }

Yashwanth MuthineniPosted Aug 19, 2015, 6:16 AM
Nice Share
sreenivasa kPosted Aug 12, 2015, 4:44 PM
really nice
Santhakumar MunuswamyPosted Aug 12, 2015, 2:56 PM
Good Article. Thanks for sharing
Sachin KaliaPosted Aug 12, 2015, 12:58 PM
Thanks everyone who has read and gave valuable remarks on this...
Chervine BhiwooPosted Aug 12, 2015, 10:52 AM
Nice Share...
Gopi ChandPosted Aug 12, 2015, 7:27 AM
Well done
Rahul PrajapatPosted Aug 12, 2015, 3:09 AM
Nice article sir
Sibeesh VenuPosted Aug 12, 2015, 3:09 AM
Nice Share...
Pankaj Kumar ChoudharyPosted Aug 11, 2015, 11:21 PM
nice share sir..........
RakeshPosted Aug 11, 2015, 2:37 PM
Good one
Shridhar SharmaPosted Aug 11, 2015, 11:23 AM
Nice role played by Extension Method in extending the Sealed class. Very Informative.
Nilesh JadavPosted Aug 11, 2015, 9:53 AM
Thanks for the work :)
Gopi ChandPosted Aug 11, 2015, 9:01 AM
Nice work
Rajeesh MenothPosted Aug 11, 2015, 8:06 AM
Good One
Karthikeyan KPosted Aug 11, 2015, 8:01 AM
Thanks for share...