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

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

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...