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");
        }

    }
}

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...