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

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

    }
}