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

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

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