How to Resolve Error "CS0273: The accessibility modifier of the accessor must be more restrictive than the property" in C#
The Compiler Error CS0273 is a logical visibility error regarding Properties and Indexers. The message reads: "The accessibility modifier of the accessor must be more restrictive than the property or indexer 'Name'".
In C#, a property defines a scope of access (e.g., public, protected). You are allowed to modify the visibility of specific accessors (get or set) individually, but only to tighten security, not to loosen it. You cannot declare a private property and then try to make its getter public. The "container" (the property) sets the maximum visibility ceiling; the accessors must be equal to or lower than that ceiling.
This guide explains the hierarchy of access modifiers and how to correctly configure asymmetric property visibility.
Understanding Visibility Hierarchy
To fix this error, you must understand which modifiers are "more restrictive" than others.
From Least Restrictive (Most Visible) to Most Restrictive:
publicprotected internalprotected/internalprivate protectedprivate
The Rule: If you place a modifier on a get or set keyword, that modifier must be lower on this list than the modifier on the property definition.
Scenario 1: Trying to Widen Access
This is the standard cause of CS0273. You define a property as protected (visible only to children), but you try to make the get accessor public (visible to everyone). This is impossible because the property itself is hidden from the public.
Example of error:
public class User
{
// The property is 'protected' (Restricted)
protected int Id
{
// ⛔️ Error CS0273: The accessibility modifier of the accessor must be
// more restrictive than the property.
// You cannot have a 'public' getter inside a 'protected' property.
public get;
set;
}
}
Scenario 2: Matching Visibility (Redundancy)
While technically a different error code in some contexts (CS0274), trying to set the accessor to the same visibility as the property is also invalid syntax for asymmetric modifiers. If the property is public, the accessors are implicitly public. You don't need to type it.
However, if you mix unrelated modifiers (like internal property and protected accessor), you might trigger CS0273 if the compiler determines the accessor is effectively wider in specific contexts.
Example of error:
internal class Config
{
// Property is 'private'
private string Key
{
// ⛔️ Error CS0273: 'internal' is less restrictive than 'private'.
// You cannot open a window in a solid wall.
internal get;
set;
}
}
Solution: Set the Maximum Visibility on the Property
The correct approach is to define the Property with the widest visibility you need (e.g., public), and then restrict the specific Accessor you want to hide (e.g., private set).
Solution
If you want a public getter and a protected setter:
public class User
{
// 1. Set the Property to the widest level needed (Public)
public int Id
{
// 2. Leave the getter as implicit (Inherits Public)
get;
// 3. Restrict the setter
// ✅ Correct: 'protected' is MORE restrictive than 'public'.
protected set;
}
}
Solution (Internal Logic)
If you want an internal getter and a private setter:
internal class Config
{
// Property is Internal
internal string Key
{
get; // Inherits Internal
// ✅ Correct: 'private' is MORE restrictive than 'internal'.
private set;
}
}
Rule of Thumb: You can only modify one of the accessors. The other one automatically takes the visibility of the property itself.
- Valid:
public int X { get; private set; } - Invalid:
public int X { public get; private set; }(Both modified)
Conclusion
CS0273 enforces logical consistency in encapsulation.
- Identify the Goal: Which part needs to be visible to the world? The Getter? The Setter? Or both?
- Set Property Level: Set the property declaration to the highest visibility level required.
- Restrict Accessor: Apply a modifier to the specific accessor (
getorset) that needs lower visibility.