HomeCSharpC# Error CS0531 – ‘member’ : interface members cannot have a definition

C# Error CS0531 – ‘member’ : interface members cannot have a definition

C# Compiler Error

CS0531 – ‘member’ : interface members cannot have a definition

Reason for the Error

You’ll get this error in your C# code when you try to provide the method definition for the methods declared with-in the interface.

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

using System;

namespace DeveloperPublishConsole1
{
    public interface Interface1
    {
        int Method1()
        {
            return 0;
        }
    }

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

You’ll receive the error code CS0531 when run the above code snippet because the interface Interface1 contains Method1 which has the definition.

Main.cs(7,13): error CS0531: `DeveloperPublishConsole1.Interface1.Method1()’: interface members cannot have a definition
Compilation failed: 1 error(s), 0 warnings

You will receive a different error code depending on the version of C# that you are running. For example, when you run this code snippet on .NET 4.7.2, you will receive the following error message.

Error CS8701 Target runtime doesn’t support default interface implementation. DeveloperPublishConsole1 C:\Users\senth\source\repos\DeveloperPublishConsole1\DeveloperPublishConsole1\Program.cs 7 Active

Error CS8370 Feature ‘default interface implementation’ is not available in C# 7.3. Please use language version 8.0 or greater. DeveloperPublishConsole1 C:\Users\senth\source\repos\DeveloperPublishConsole1\DeveloperPublishConsole1\Program.cs 7 Active

Solution

In C#, the methods that are declared in an interface must be implemented in a class and not in the interface itself.

To fix this error in your C# code, you will need to remove the method definition.

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