Skip to main content

How to Resolve Error "CS0154: The property or indexer cannot be used in this context because it lacks the get accessor" in C#

The Compiler Error CS0154 is an access capability error. The message reads: "The property or indexer 'PropertyName' cannot be used in this context because it lacks the get accessor".

In C#, Properties and Indexers are composed of two distinct accessors:

  • get: Used to read (retrieve) the value.
  • set (or init): Used to write (assign) the value.

If you define a property that only has a set accessor, it becomes a Write-Only property. You can assign values to it, but you cannot read values from it. This error occurs when your code attempts to read a value from a property that was defined as Write-Only.

Understanding Write-Only Properties

While uncommon, C# allows you to define properties that can only be written to. This is sometimes used for sensitive data (like passwords) or hardware configuration where you send commands but cannot query the state.

Definition:

// Write-Only Property
public int MyValue
{
set { /* logic */ }
// No 'get' block exists
}

Usage:

  • obj.MyValue = 10; (Valid: Uses set)
  • int x = obj.MyValue; (Invalid: Tries to use nonexistent get)

Scenario: Reading a Write-Only Property

The most common cause is accidentally omitting the get keyword when defining a property, or misunderstanding how a Write-Only property works.

Example:

public class Logger
{
private string _filePath;

// This property is Write-Only
public string FilePath
{
set { _filePath = value; }
}

public void PrintStatus()
{
// ⛔️ Error CS0154: The property 'Logger.FilePath' cannot be used
// because it lacks the get accessor.
// We are trying to READ 'FilePath' to print it.
System.Console.WriteLine($"Logging to: {FilePath}");
}
}

Solution 1: Add a Get Accessor (Make it Readable)

If the property is intended to be read, you simply need to add the get accessor.

Example:

public class Logger
{
private string _filePath;

public string FilePath
{
// ✅ Correct: Added the 'get' accessor
get { return _filePath; }
set { _filePath = value; }
}

public void PrintStatus()
{
// Now valid because 'get' exists
System.Console.WriteLine($"Logging to: {FilePath}");
}
}
note

Private Getters: If you want the property to be readable only inside the class but writeable from outside, you can use access modifiers: get; public set;. However, if you omit get entirely, nobody (not even the class itself) can read the property directly.

Solution 2: Access the Backing Field (Internal Logic)

Sometimes a property is intentionally Write-Only (e.g., a "Password" field that sets a hash). If you are writing code inside that class and need to read the value, do not try to read the property. Instead, read the private backing field that stores the data.

Solution: access the private variable (_variable), not the public property (Variable).

public class Authenticator
{
private string _passwordHash;

// Explicitly Write-Only: Outside world can set it, but not read it.
public string Password
{
set { _passwordHash = Encrypt(value); }
}

public bool Verify(string input)
{
// ⛔️ Incorrect: if (this.Password == ...) -> Error CS0154

// ✅ Correct: access the backing field '_passwordHash' directly
return _passwordHash == Encrypt(input);
}

private string Encrypt(string s) => s + "_hashed"; // Dummy logic
}

Conclusion

CS0154 ensures you don't try to read data from a "black box" input.

  1. Check the Definition: Does your property block have a get { ... } or get;?
  2. If intended to be readable: Add the get accessor.
  3. If intended to be Write-Only: Ensure you aren't trying to read it. If you are inside the class, read the underlying private field instead.