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

C# Error CS0501 – ‘member function’ must declare a body because it is not marked abstract, extern, or partial

C# Compiler Error

CS0501 – ‘member function’ must declare a body because it is not marked abstract, extern, or partial

Reason for the Error

You’ll get this error in your C# code when you try to declare a function without a body.

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

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

    }
}

You’ll receive the error code CS0501 because the C# compiler has detected that you have declared a non-abstract method SetId() without the method body.

Error CS0501 ‘Employee.SetId()’ must declare a body because it is not marked abstract, extern, or partial DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 6 Active

C# Error CS0501 – 'member function' must declare a body because it is not marked abstract, extern, or partial

Solution

Non-abstract methods in C# must have a method body or implementation. You can fix this error in your C# program by adding the implementation or method body when declaring the function.

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

    }
}