HomeCSharpC# Error CS0470 – ‘method’ cannot implement interface accessor ‘accessor’ for type ‘type’. Use an explicit interface implementation

C# Error CS0470 – ‘method’ cannot implement interface accessor ‘accessor’ for type ‘type’. Use an explicit interface implementation

C# Compiler Error

CS0470 – Method ‘method’ cannot implement interface accessor ‘accessor’ for type ‘type’. Use an explicit interface implementation

Reason for the Error

You’ll get this error in your C# code when an accessor is trying to implement an interface.

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

using System;
namespace DeveloperPublishNamespace
{
    interface IEmployee
    {
        int Id { get; }
    }

    class MyClass : IEmployee
    {
        // This results in the error CS0470 
        public int get_Id() { 
            return 0;
        }   
    }
    class Program
    {      
        static void Main(string[] args)
        {
            Console.WriteLine("No Error");
        }

    }
}

You’ll receive the error code CS0470 when you build the above C# code because the C# compiler has detected that you are trying to implement interface accessor of Id property.

Error CS0470 Method ‘MyClass.get_Id()’ cannot implement interface accessor ‘IEmployee.Id.get’ for type ‘MyClass’. Use an explicit interface implementation. DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 12 Active

Solution

You can fix this error in your C# program by using the explicit interface implementation as shown below.

using System;
namespace DeveloperPublishNamespace
{
    interface IEmployee
    {
        int Id { get; }
    }

    class MyClass : IEmployee
    {
        public int Id
        {
            get;
        }
    }
    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...