Skip to main content

How to Resolve Error "CS0272: The property or indexer cannot be used because the set accessor is inaccessible" in C#

The Compiler Error CS0272 is an Encapsulation/Access error. The message reads: "The property or indexer 'Property' cannot be used in this context because the set accessor is inaccessible".

This error is the counterpart to CS0271. It occurs when a property is publicly visible (you can read it via get), but the ability to modify it (set) is restricted by an Access Modifier like private, protected, or internal. If you try to assign a value to such a property from a location prohibited by that modifier (e.g., attempting to set a private set property from an external class), the compiler blocks the operation to preserve data integrity.

This guide explains why developers restrict setters and how to modify these properties correctly.

Understanding Asymmetric Accessors

In C#, you can apply different access levels to the get and set parts of a property. A common pattern for "immutable" or "managed" data is:

public int Id { get; private set; }
  • get: Inherits the public modifier. Anyone can read the ID.
  • set: Is explicitly private. Only the class itself can change the ID.

If the Main method or another class tries to write obj.Id = 5;, CS0272 is raised because Main does not have access to private members of obj.

Scenario 1: The Private Setter (Read-Only to Public)

This is the most common occurrence. You want a property to be read-only to the outside world, but you accidentally try to set it from the outside.

Example of error:

public class User
{
// Public Read, Private Write
public string Username { get; private set; }

public User(string name)
{
Username = name; // Allowed: Constructor is inside the class
}
}

public class Program
{
static void Main()
{
var u = new User("Alice");

// ⛔️ Error CS0272: The property 'User.Username' cannot be used
// because the set accessor is inaccessible.
u.Username = "Bob";
}
}

Scenario 2: The Protected Setter

The protected modifier allows access to the class itself and derived classes (children). It does not allow access via an object instance in an unrelated class.

Example of error: trying to set a protected property from Main or a non-derived class.

public class BaseConfig
{
public int Timeout { get; protected set; }
}

public class Program
{
static void Main()
{
var config = new BaseConfig();

// ⛔️ Error CS0272: 'set' is protected.
// Program is not inside BaseConfig, nor does it inherit from it
// in a way that allows instance access here.
config.Timeout = 100;
}
}

Solution 1: Use Constructors or Methods

If the setter is private/protected for a reason (encapsulation), you should not try to circumvent it. Instead, use the public API provided by the class (Constructors or Methods) to change the state.

Solution: update the value using a specific method provided by the class.

public class User
{
public string Username { get; private set; }

public User(string name)
{
Username = name;
}

// ✅ Correct: Expose a public method to perform the change safely.
public void Rename(string newName)
{
if (!string.IsNullOrWhiteSpace(newName))
{
Username = newName;
}
}
}

public class Program
{
static void Main()
{
var u = new User("Alice");

// Use the method instead of the property setter
u.Rename("Bob");
}
}

Solution 2: Change Access Modifiers

If the property should be modifiable by everyone (i.e., it is a simple Data Transfer Object or DTO), then the access modifier is too restrictive. Change it to public.

Solution: remove private or protected from the setter.

public class User
{
// ✅ Correct: Both get and set are Public.
public string Username { get; set; }
}

public class Program
{
static void Main()
{
var u = new User { Username = "Alice" };

// This is now allowed
u.Username = "Bob";
}
}
note

Internal Setters: If you want the property to be writable by any class in your own project, but read-only to external libraries referencing your DLL, use internal set. public string Name { get; internal set; }

Conclusion

CS0272 protects your object's internal state.

  1. Check the Definition: Look at the property. Does it say private set or protected set?
  2. Respect Encapsulation: If it is private, look for a Constructor or a Method (like Update() or Set...()) to modify the value.
  3. Refactor: If it should be publicly writable, remove the restrictive modifier from the set accessor.