C# Sealed

In C#, the sealed keyword is used to restrict inheritance of a class or to prevent further overriding of a method in a derived class. When applied to a class, it prevents other classes from inheriting from it. When applied to a method, it prevents derived classes from overriding that method.

Here’s an example of using the sealed keyword with a class:

public class MyBaseClass
{
    // ...
}

public sealed class MyDerivedClass : MyBaseClass
{
    // ...
}

// The following line would cause a compile-time error since MyDerivedClass is sealed
public class MyAnotherClass : MyDerivedClass
{
    // ...
}

In this example, MyDerivedClass is marked as sealed, so it cannot be inherited by any other class. Attempting to derive a class from MyDerivedClass, like MyAnotherClass, would result in a compilation error.

Here’s an example of using the sealed keyword with a method:

public class MyBaseClass
{
    public virtual void MyMethod()
    {
        // ...
    }
}

public class MyDerivedClass : MyBaseClass
{
    public sealed override void MyMethod()
    {
        // ...
    }
}

public class MyAnotherClass : MyDerivedClass
{
    // The following line would cause a compile-time error since MyMethod is sealed
    public override void MyMethod()
    {
        // ...
    }
}

In this example, the MyDerivedClass class overrides the MyMethod method and seals it using the sealed keyword. This means that no further derived classes, like MyAnotherClass, can override the MyMethod method.

Note that the sealed keyword can only be applied to methods that are marked as virtual, abstract, or override. It cannot be applied to static methods or non-virtual methods.

C# Sealed class:

In C#, a sealed class is a class that cannot be inherited or used as a base class for other classes. When a class is marked as sealed, it indicates that it is the final implementation of the class and cannot be extended further.

Here’s an example of a sealed class:

public sealed class MySealedClass
{
    // Class members and methods
    // ...
}

In this example, MySealedClass is marked as sealed. As a result, other classes cannot inherit from it. If you try to derive a class from a sealed class, you will get a compile-time error.

public class MyDerivedClass : MySealedClass // This will cause a compile-time error
{
    // ...
}

The primary purpose of marking a class as sealed is to prevent further inheritance, ensuring that the implementation cannot be extended or modified in derived classes. Sealing a class can be useful when you have a specific implementation that you want to protect or when you want to provide a fixed set of functionality without the possibility of further extension.

It’s important to note that the sealed keyword can only be applied to classes that are not abstract. Abstract classes cannot be sealed because they are designed to be inherited and extended by derived classes.

C# Sealed method:

In C#, the sealed keyword is not used to seal individual methods. It is only used to seal classes to prevent inheritance.

Methods in C# can be marked as sealed when they override a virtual method in a base class. The sealed keyword applied to a method indicates that the method cannot be overridden in any derived class.

Here’s an example of using the sealed keyword with a method:

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

public class MyAnotherClass : MyDerivedClass
{
    // The following line would cause a compile-time error since MyMethod is sealed
    public override void MyMethod()
    {
        Console.WriteLine("Another class method");
    }
}

In this example, the MyDerivedClass class overrides the MyMethod method from the base class and seals it using the sealed keyword. This means that no further derived classes, like MyAnotherClass, can override the MyMethod method.

It’s important to note that the sealed keyword can only be applied to methods that are marked as override. Sealing a method indicates that the method implementation is final and cannot be further overridden in derived classes.