Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

C# Error CS0503 – The abstract method ‘method’ cannot be marked virtual

C# Compiler Error

CS0503 – The abstract method ‘method’ cannot be marked virtual

Reason for the Error

You’ll get this error in your C# code when you have marked an abstract method as virtual.

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

using System;
namespace DeveloperPublishNamespace
{
    public abstract class Employee
    {
        public virtual abstract void SetId();
    }

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

    }
}

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

Error CS0503 The abstract method ‘Employee.SetId()’ cannot be marked virtual DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 6 Active

C# Error CS0503 – The abstract method 'method' cannot be marked virtual

Solution

In C# programming language, you cannot declare a class member as both abstract and virtual. You can fix this error in your C# program by ensuring that you remove the virtual keyword for the abstract method since abstract method implies virtual.

using System;
namespace DeveloperPublishNamespace
{
    public abstract class Employee
    {
        public abstract void SetId();
    }

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

    }
}