SoFunction
Updated on 2025-03-07

Easy to learn C# sealing class

Overview of seals

Not all classes can be inherited, but sealed classes are in C# language. In C# we can declare the class as sealed. This means that the class cannot be inherited. If you want to inherit, the compiler will definitely report an error. When marking a class or method as sealed, the most likely situation is: if you want to operate on a library, class, or other classes you wrote yourself, rewriting certain functions will lead to a compilation error. Classes or methods can also be marked as sealed for commercial reasons in case a third party violates the registration agreement. But in general, be careful when marking a class or method as sealed, as doing so will limit its use. Even if you don't want it to inherit a class or rewrite a member of a class, it is still possible that at some point in the future, someone will encounter unpredictable situations. The .NET base class library uses sealed classes extensively, making them inaccessible to third-party developers who want to derive their own classes from them.

The declaration format of the seal class is:

Access modifier sealed class class name: base class or interface
       {
Available
       }

The access modifier, base class or interface are optional.

Overview of sealing methods

When an instance method declaration contains a sealed modifier, the method is called a sealing method. If the instance method declaration contains a sealed modifier, it must contain an override modifier. Using the sealed modifier prevents the derived class from rewriting the method further. To use the sealed keyword in a method, it must now be declared as overridden on the base class. If you do not want overridden methods or attributes on the base class, don't declare them as virtual.

Access modifier sealed override method name (parameter list)
      {
Method
      }

The access modifier and parameter list are optional.

Let's take a look at the example

using System; 
using ; 
using ; 
using ; 
using ; 
 
namespace qq 
{ 
  class class1 
  { 
    public virtual void seaText() 
    { 
      ("This is an unsealed method!"); 
    } 
  } 
  sealed class class2 : class1 
  { 
    public sealed override void seaText() 
    { 
      ("This is a sealing method!"); 
    } 
  } 
  /*class class3: class2//Sealing class cannot be inherited
   {
     public override void seaText()//Cannot rewrite the seal method
     {
       ("This is a method that cannot be run!");
     }
   }*/ 
  class Program 
  { 
    static void Main(string[] args) 
    { 
      class2 c2 = new class2(); 
      (); 
      (); 
    } 
  } 
}

A very simple output result:This is a sealing method

using System; 
using ; 
using ; 
using ; 
using ; 
 
namespace qq 
{ 
  sealed class Person 
  { 
    public void print_Person_name() 
    { 
      ("Zhang San"); 
    } 
  } 
  class Student : Person//Sealing cannot be inherited  { 
  } 
  class Program 
  { 
    static void Main(string[] args) 
    { 
      Student s1 = new Student(); 
      s1.Print_Person_name();//The method of sealed class cannot be called      (); 
    } 
  } 
}

This is aError instance, This situation cannot occur when we use sealing classes, and we generally need to use such keywords less.

The above is the full introduction to the sealing class of C#, and I hope it will be helpful to everyone's learning.