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

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...