HomeCSharpC# Error CS0500 – ‘class member’ cannot declare a body because it is marked abstract

C# Error CS0500 – ‘class member’ cannot declare a body because it is marked abstract

C# Compiler Error

CS0500 – ‘class member’ cannot declare a body because it is marked abstract

Reason for the Error

You’ll get this error in your C# code when you try to declare a body for a function that is marked as abstract.

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

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

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

    }
}

You’ll receive the error code CS0500 when you build the above C# code because the C# compiler has detected that you have specified the method body for the abstract method “SetId”.

Error CS0500 ‘Employee.SetId()’ cannot declare a body because it is marked abstract DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 6 Active

C# Error CS0500 – 'class member' cannot declare a body because it is marked abstract

Solution

An abstract method in C# cannot contain its implementation. You can fix this error in your C# program by removing the implementation or method body in the declaration.

using System;
namespace DeveloperPublishNamespace
{
    abstract public class Employee
    {
        abstract public void SetId();
    }
    class Program
    {      
        static void Main(string[] args)
        {
            Console.WriteLine("No Error");
        }

    }
}

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...