C# Error CS0502 – ‘member’ cannot be both abstract and sealed

C# Compiler Error

CS0502 – ‘member’ cannot be both abstract and sealed

Reason for the Error

You’ll get this error in your C# code when you try to declare a class member as both abstract and sealed.

For example, let’s try to compile the below C# code snippet.

using System;
namespace DeveloperPublishNamespace
{
    public abstract class Employee
    {
        public abstract void SetId();
    }
    public class ContractEmployee : Employee
    {
        public abstract sealed override void SetId()
        {

        }
    }
    class Program
    {      
        static void Main(string[] args)
        {
            Console.WriteLine("No Error");
        }

    }
}

You’ll receive the error code CS0502 because the C# compiler has detected that you have declared the SetId() in the ContractEmployee class as both abstract and sealed.

Error CS0502 ‘ContractEmployee.SetId()’ cannot be both abstract and sealed DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 10 Active

C# Error CS0502 – 'member' cannot be both abstract and sealed

Solution

In C# programming language, you cannot declare a class member as both abstract and sealed. You can fix this error in your C# program by ensuring that the class member is either abstract or sealed but not both.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...