Home > Backend Development > C#.Net Tutorial > Final keyword in C#

Final keyword in C#

王林
Release: 2023-08-26 08:33:15
forward
1116 people have browsed it

C# 中的 Final 关键字

Java has the final keyword, but C# does not have its implementation. For the same implementation, use the seal keyword.

Using seal, you can prevent methods from being overridden. When you use the sealed modifier on a method in C#, the method loses its override functionality. The Sealed method should be part of the derived class and the method must be an overridden method.

The following example does not allow you to override the method display() because it has the seal modifier of the ClassTwo derived class.

ClassOne is our base class, while ClassTwo and ClassThree are derived classes -

Example

class ClassOne {
   public virtual void display() {
         Console.WriteLine("Baseclass");
   }
}

class ClassTwo : ClassOne {
   public sealed override void display() {
      Console.WriteLine("ClassTwo:derivedClass");
   }
}

class ClassThree : ClassTwo {
   public override void display() {
      Console.WriteLine("ClassThree: Another Derived Class");
   }
}
Copy after login

The above is the detailed content of Final keyword in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template