C# Error CS0277 – ‘class’ does not implement interface member ‘accessor’. ‘class accessor’ is not public

C# Compiler Error

CS0277 – ‘class’ does not implement interface member ‘accessor’. ‘class accessor’ is not public

Reason for the Error

You’ll get this error in your C# code when you are implementing a property of an interface in a class but the implemented property in the class in NOT public.

For example, lets try to compile the below code snippet.

using System;

namespace DeveloperPublishNamespace
{
    public interface IEmployee
    {
        public string Name { get; set; }
    }
    public class Employee : IEmployee
    {
        public string Name 
        { 
            get => throw new NotImplementedException(); 
            protected set => throw new NotImplementedException(); 
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

You’ll receive the error code CS0277 because you are implementing the property Name from the interface IEmployee but in the class Employee, you have specified the access modifier as protected for the Name property.

Error CS0277 ‘Employee’ does not implement interface member ‘IEmployee.Name.set’. ‘Employee.Name.set’ is not public. DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 9 Active

C# Error CS0277 – 'class' does not implement interface member 'accessor'. 'class accessor' is not public

Solution

In C#, the methods, properties and the indexers that implement the interface should have the public access modifiers.

You can fix this error in your C# program by removing the explicit access modifier (protected in our case) on the property.

using System;

namespace DeveloperPublishNamespace
{
    public interface IEmployee
    {
        public string Name { get; set; }
    }
    public class Employee : IEmployee
    {
        public string Name 
        { 
            get => throw new NotImplementedException(); 
            set => throw new NotImplementedException(); 
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

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