Sealed
Sealed can be applied to class and class methods. Sealed methods cannot be overridden and sealed class cannot be inherited.
static void Main() { House mansion = new House { Name="McMansion", Mortgage=250000 }; Console.WriteLine (mansion.Liability); // 250000 } public class Asset { public string Name; public virtual decimal Liability => 0; // Virtual } public class House : Asset { public decimal Mortgage; public sealed override decimal Liability => Mortgage; // Overridden + sealed } // You can also seal the class itself, implicitly sealing all the virtual functions: public sealed class Stock : Asset { /* ... */ }
Sealing Methods
If you want to override a method than the base method must have modifier marked either virtual, abstract, override.
If you want to seal the overridden method in derived class so that it cannot be further overridden in the inherited class then mark it as sealed.
using System; namespace Rextester { public class Program { public static void Main(string[] args) { CheckingAccount credit = new CheckingAccount(); credit.ShowCredit(); credit.SavingCredit(); } } public class CreditBase { public void ShowCredit() { Console.WriteLine("Base show credit"); } //can be overridden public virtual void SavingCredit() { Console.WriteLine("Base saving credit"); } } public class SavingAccount:CreditBase { //preventing further to be overridden public override sealed void SavingCredit() { Console.WriteLine("Saving saving credit"); } } public class CheckingAccount:SavingAccount { //error not possible public override sealed void SavingCredit() { Console.WriteLine("Checking saving credit"); } } }