C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
The sealed
keyword in C# is used to declare a sealed class or sealed method, preventing further inheritance or method overriding. This tutorial will cover the following topics related to the sealed
keyword in C#:
Let's begin!
A sealed class is a class that cannot be inherited. To declare a sealed class, use the sealed
keyword:
public sealed class MyBaseClass { // Class members go here }
Attempting to inherit from a sealed class will result in a compile-time error:
public class MyDerivedClass : MyBaseClass { // This will cause a compile-time error: 'MyDerivedClass': cannot derive from sealed type 'MyBaseClass' }
Sealed classes are useful when you want to prevent further inheritance for a specific class, ensuring that the class hierarchy remains fixed and cannot be extended.
A sealed method is a method that cannot be overridden in a derived class. To declare a sealed method, use the sealed
keyword with an override
keyword:
public class MyBaseClass { public virtual void MyMethod() { Console.WriteLine("Base class method"); } } public class MyDerivedClass : MyBaseClass { public sealed override void MyMethod() { Console.WriteLine("Derived class method"); } }
In this example, MyDerivedClass
overrides the MyMethod
method from MyBaseClass
and marks it as sealed
. This means that any further derived classes cannot override the MyMethod
method:
public class MyFurtherDerivedClass : MyDerivedClass { public override void MyMethod() // This will cause a compile-time error: 'MyFurtherDerivedClass.MyMethod()': cannot override inherited member 'MyDerivedClass.MyMethod()' because it is sealed { Console.WriteLine("Further derived class method"); } }
Sealed methods are useful when you want to allow inheritance for a specific class but prevent a particular method from being overridden in derived classes.
That's it! You've now learned how to use the sealed
keyword in C# to declare sealed classes and sealed methods, preventing further inheritance or method overriding. The sealed
keyword is useful for ensuring that a class hierarchy remains fixed and specific methods cannot be overridden in derived classes, providing more control over the design of your classes and methods.
How to use sealed classes in C#
Sealed classes prevent further inheritance, making them a final version.
sealed class SealedClass { // Class members }
Sealing methods with the sealed keyword in C#
The sealed
keyword can be applied to methods to prevent overriding.
class BaseClass { public virtual void MethodToOverride() { } } class DerivedClass : BaseClass { public sealed override void MethodToOverride() { } }
C# sealed keyword and extension methods
Sealed classes can still have extension methods, providing additional functionality.
static class SealedClassExtensions { public static void NewMethod(this SealedClass sealedInstance) { // Extension method logic } }