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

    }
}

Leave A Reply

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

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