C# Error CS0239 – ‘member’ : cannot override inherited member ‘inherited member’ because it is sealed

C# Compiler Error

CS0239 – ‘member’ : cannot override inherited member ‘inherited member’ because it is sealed

Reason for the Error

You will receive this error in your C# program when you have one of the member override a sealed inherited member which is not allowed.

For example, try to compile the below code snippet.

namespace DeveloperPubNamespace
{
    abstract class BaseEmployee
    {
        public abstract void SetId();
    }
    class Employee : BaseEmployee
    {
        public sealed override void SetId()
        { 
        }
    }
    class ContractEmployee : Employee
    {
        public override void SetId()
        {
        }
    }
    class Program
    {
        public static void Main()
        {
        }
    }
}

This program will result with the C# error code CS0239 because the SetId() in the Employee class is marked as sealed and we are trying to override it inside the ContractEmployee class.

Error CS0239 ‘ContractEmployee.SetId()’: cannot override inherited member ‘Employee.SetId()’ because it is sealed DeveloperPublish C:\Users\SenthilBalu\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 15 Active

C# Error CS0239 – 'member' : cannot override inherited member 'inherited member' because it is sealed

Solution

To fix the error code CS0239 in C#, you should not be overriding a sealed inherited member as this is not allowed.