C# Error CS0273 – The accessibility modifier of the ‘property_accessor’ accessor must be more restrictive than the property or indexer ‘property’

C# Compiler Error

CS0273 – The accessibility modifier of the ‘property_accessor’ accessor must be more restrictive than the property or indexer ‘property’

Reason for the Error

You’ll get this error in your C# code when you have defined the accessibility of a accessor that isn’t less restrictive than the accessor of the property or indexer itself.

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

using System;

namespace DeveloperPubNamespace
{
    public class Employee
    {
        private int _id;

        public int Id
        {
            public set { _id = value; }
            get { return _id; }
        }

    }
    class Program
    {
        public static void Main()
        {
            Console.WriteLine("No Error");
        }
    }
}

You’ll receive the error code CS0273 because you are using the public accessor for the Set accessor of the property Id while the property “Id” itself is still public. The C# compiler expects you to have a restricted accessor in this case.

Error CS0273 The accessibility modifier of the ‘Employee.Id.set’ accessor must be more restrictive than the property or indexer ‘Employee.Id’ DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 11 Active

C# Error CS0273 – The accessibility modifier of the 'property_accessor' accessor must be more restrictive than the property or indexer 'property'

Solution

You can fix this error in your C# program by decreasing the accessibility of the set accessor (Eg : changing the access modifier to private/internal).

using System;

namespace DeveloperPubNamespace
{
    public class Employee
    {
        private int _id;

        public int Id
        {
            private set { _id = value; }
            get { return _id; }
        }

    }
    class Program
    {
        public static void Main()
        {
            Console.WriteLine("No Error");
        }
    }
}

Leave A Reply

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

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