Home » General » C# Sealed Method

C# Sealed Method

The keyword sealed is used in class definition to prevent being derived from. When it’s apply to methods, it mean that the method cannot be overrided.

Well, in fact, method can only be overrided when the virtual keyword is used so why sealed is required? It’s required when a class derive from a class that had a virtual method. This one will override and an overrided method can be also overrided. To prevent that, the override method can use the sealed keyword.

class A
{
   public virtual void F() {      Console.WriteLine("A.F");   }
   public virtual void G() {      Console.WriteLine("A.G");   }
}
class B: A
{
   sealed override public void F() {      Console.WriteLine("B.F");   } 
   override public void G() {      Console.WriteLine("B.G");   } 
}
class C: B
{
   override public void G() {      Console.WriteLine("C.G");   } 
}

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.