How to Resolve Error "CS0276: Accessibility modifiers on accessors may only be used if the property has both a get and a set accessor" in C#
The Compiler Error CS0276 is a syntax redundancy error regarding Property Accessors. The message reads: " 'Property': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor".
In C#, you can place an access modifier (like private or protected) on an individual accessor (get or set) to give it a different visibility than the property itself. However, this is only logical if there are two accessors to differentiate between. If a property has only one accessor (e.g., it is Read-Only), modifying that single accessor is redundant; you should simply apply that modifier to the property definition itself.
This guide explains the logic of accessor modifiers and how to fix this syntax error.
Understanding Accessor Modifiers
Access modifiers on accessors are designed for Asymmetric Accessibility.
- Symmetric (Standard):
public int X { get; set; }(Both are public). - Asymmetric:
public int X { get; private set; }(Public Read, Private Write).
If you have a property with only a getter:
public int X { private get; }
This is illogical. If the property is public, but the only way to access it (get) is private, then the entire property is effectively private. The compiler forces you to declare the property itself as private to avoid confusion.
Scenario 1: Restricting a Read-Only Property
This error commonly occurs when a developer wants to make a property internal or private but applies the modifier to the getter instead of the property line.
Example of error:
public class User
{
// ⛔️ Error CS0276: 'User.Id': accessibility modifiers on accessors may only be used
// if the property or indexer has both a get and a set accessor.
// Explanation: There is no 'set'. The 'get' IS the property.
public int Id { private get; }
}
Solution 1: Move Modifier to the Property
If the property only has one accessor, the visibility of that accessor determines the visibility of the whole property. Move the keyword from inside the braces to the start of the declaration.
Solution:
public class User
{
// ✅ Correct: The property itself is now private.
// The 'get' accessor inherits 'private' automatically.
private int Id { get; }
}
Solution 2: Add the Missing Accessor
If you actually intended to have different levels of access (e.g., Public Read, Private Write), you are likely missing the set (or init) accessor.
Solution: add the setter to enable asymmetric accessibility.
public class User
{
// ✅ Correct: Now we have BOTH accessors.
// The Property is Public.
// The Getter inherits Public.
// The Setter is restricted to Private.
public int Id { get; private set; }
}
Expression Bodied Members:
This rule also applies to arrow syntax =>.
public int Id => 5; is inherently a Read-Only property. You cannot add modifiers to it because there is no set block to contrast against.
Conclusion
CS0276 prevents redundant or confusing syntax.
- Count Accessors: Do you have both
getandset? - If Only One: You cannot put a modifier inside the
{ }. Applypublic,private, orinternalto the Property line itself. - If Two: You are allowed to restrict one of them (e.g.,
private set).