Sealed class and Sealed method in C#.Net


In this post, we will discuss Sealed class and Sealed method in C#.Net.


Sealed class:

C# allows classes and methods to be declared as sealed. In the case of a class, this means you can’t inherit from that class. If you want no one should extend your class then you can make that class as a Sealed class.

To make a class as Sealed we have to use the Sealed keyword.

sealed class MySealedClass
{
// Code will go here
}

Now suppose you want to inherit this class in any other class like below, then it will give a compilation error.

class MyDerivedClass: MySealedClass
{
// Code will go here
}

Sealed Method:

Similarly, C#.Net allows us to make methods as Sealed so that no other class can use the method.

sealed class MySealedClass
{
public sealed override void MyMethod()
{
//Code will go here
}
}

But if you try to access the sealed method in the child class then it will give a compailation error.

class MyDerivedClass: MySealedClass
{
public override void MyMethod()
{
//Code will go here
}
}

In order to use the sealed keyword on a method or property, it must have first been overridden from a base class.

Also, you can check out:

Method Overloading in C#.Net



– Read-only textbox value lost on postback asp.net

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →