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