HomeCSharpC# Error CS0274 – Cannot specify accessibility modifiers for both accessors of the property or indexer ‘property/indexer’

C# Error CS0274 – Cannot specify accessibility modifiers for both accessors of the property or indexer ‘property/indexer’

C# Compiler Error

CS0274 – Cannot specify accessibility modifiers for both accessors of the property or indexer ‘property/indexer’

Reason for the Error

You’ll get this error in your C# code when you have specified the access modifiers for both the get and the set accessors.

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

using System;

namespace DeveloperPubNamespace
{
    public class Employee
    {
        private int _id;

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

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

You’ll receive the error code CS0273 because you have specified the access modifiers “private” for set and “internal” for the get accessor and this is not allowed by the C# compiler.

Error CS0274 Cannot specify accessibility modifiers for both accessors of the property or indexer ‘Employee.Id’ DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 9 Active

C# Error CS0274 – Cannot specify accessibility modifiers for both accessors of the property or indexer 'property/indexer'

Solution

You can fix this error in your C# program by specifying the access modifier on only one of the two accessors as shown below.

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

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