C# Error CS0546 – ‘accessor’ : cannot override because ‘property’ does not have an overridable set accessor

C# Compiler Error

CS0546 – ‘accessor’ : cannot override because ‘property’ does not have an overridable set accessor

Reason for the Error

You will get this error in your C# code when you attempt to override a property that does not have an set accessor.

For example, let’s compile the below C# program

using System;

namespace DeveloperPublishConsoleCore
{
    public class Employee
    {
        public virtual int Id
        {
            get { return 0; }
        }
    }

    public class Contract : Employee
    {
        public override int Id
        {
            get { 
                return 0; 
            }
            set { }  
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("DeveloperPublish Hello World!");
        }
    }
}

You will receive the error code CS0545 because the class Employee contains a Virtual property “Id” without an set accessor. You are attempting to override it in the Contract class with the set accessor.

Error CS0546 ‘Contract.Id.set’: cannot override because ‘Employee.Id’ does not have an overridable set accessor DeveloperPublishConsoleCore C:\Users\senth\source\repos\DeveloperPublishConsoleCore\DeveloperPublishConsoleCore\Program.cs 20 Active

C# Error CS0546 – 'accessor' : cannot override because 'property' does not have an overridable set accessor

Solution

To fix this error in your C# program, you can add the set accessor in the base class or remove the set accessor from the derived or child class. Alternatively, you can use the new keyword to hide the base class property.

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