Skip to main content

How to Resolve Error "CS0274: Cannot specify accessibility modifiers for both accessors of the property or indexer" in C#

The Compiler Error CS0274 is a syntax error regarding Property Access Modifiers. The message reads: "Cannot specify accessibility modifiers for both accessors of the property or indexer".

In C#, a property has a main accessibility level (declared on the property itself). You are allowed to restrict one of the accessors (get or set) to be less accessible than the property, but you cannot place modifiers on both. At least one accessor must implicitly inherit the accessibility defined on the property to maintain logical consistency.

This guide explains the rules of asymmetric accessor accessibility and how to define them correctly.

Understanding Accessor Inheritance

When you declare a property, you set the "Default" visibility:

public int Score { get; set; }

Here, public applies to both.

If you want to change one, you modify the exception, not the rule:

public int Score { get; private set; }

Here, get inherits public, and set is explicitly private.

If you modify both, the compiler asks: "If neither accessor uses the property's declared visibility, why is the property declared that way?"

Scenario 1: Modifying Both Accessors

This often happens when a developer tries to create a specific encapsulation combination (e.g., protected read, private write) but declares the property itself as public.

Example of error:

public class User
{
// ⛔️ Error CS0274: Cannot specify accessibility modifiers for both accessors.
// The property is 'public', but we tried to manually set 'protected' AND 'private'.
// Nothing is left to match 'public'.
public int Id
{
protected get;
private set;
}
}

Scenario 2: Redundant Modifiers

Even if one of the modifiers matches the property declaration, you are not allowed to type it out if you are also modifying the other one. C# enforces implicit inheritance for the matching accessor.

Example of error:

public class Config
{
// ⛔️ Error CS0274: Even though 'public get' matches the property,
// you cannot put modifiers on BOTH get and set.
public string Key
{
public get;
private set;
}
}

Solution: Determine the Widest Visibility

To fix this error, identify which accessor needs to be the most visible.

  1. Apply that visibility level to the Property definition.
  2. Leave that accessor blank (no modifier).
  3. Apply the restrictive modifier to the other accessor.

Case A: Protected Read, Private Write

If you want get to be protected and set to be private:

public class User
{
// 1. 'protected' is the widest visibility needed. Set property to 'protected'.
protected int Id
{
// 2. Leave 'get' blank. It inherits 'protected'.
get;

// 3. Restrict 'set' to 'private'.
// ✅ Correct
private set;
}
}

Case B: Public Read, Private Write

If you want get to be public and set to be private:

public class Config
{
// 1. Set property to 'public'.
public string Key
{
// 2. Remove 'public' from get. It is implicit.
get;

// 3. Keep 'private' on set.
// ✅ Correct
private set;
}
}
note

Rule of Thumb: Never type an access modifier inside the { } brackets that matches the modifier outside the brackets.

Conclusion

CS0274 ensures concise and logical property definitions.

  1. Identify Goals: What visibility do you need for get? What do you need for set?
  2. Pick the Winner: Whichever one is more visible (e.g., public > protected > private) becomes the modifier for the Property itself.
  3. Restrict the Loser: Apply the modifier only to the less visible accessor. Leave the other one plain.