How to Resolve Error "CS0548: property or indexer must have at least one accessor" in C#
The Compiler Error CS0548 is a syntax declaration error. The message reads: "'PropertyName' : property or indexer must have at least one accessor".
In C#, a Property is distinct from a Field.
- A Field (
public int x;) is a direct storage location. - A Property (
public int X { get; set; }) is a pair of methods (accessors) that behave like a field.
When you declare a property using curly braces { }, the compiler expects you to define how to access that property. You must provide at least a get (read), a set (write), or an init (initialize) accessor. A property defined with empty braces is functionally useless because it cannot be read from or written to.
This guide explains how to correctly define properties and indexers.
Understanding Accessors
Accessors are the gateways to your property:
get: Defines how to retrieve the value.set: Defines how to assign a value.init(C# 9+): Defines how to assign a value only during object initialization.
If you declare a property like public int Score { }, you have created a named entity that exposes no gateways. It creates a syntax error because it fulfills no purpose.
Scenario 1: The Empty Property Block
This error usually happens when a developer forgets to type the get; set; shorthand (Auto-Implemented Properties) or intends to write the logic later and leaves the braces empty.
Example of error
public class User
{
// ⛔️ Error CS0548: 'User.Name' : property or indexer must have at least one accessor.
// Curly braces indicate a property, but it is empty.
public string Name { }
}
Solution: Add Accessors
You must decide how this property behaves.
Option A: Auto-Implemented Property
If you just want a simple storage slot (like a field), use the shorthand syntax.
public class User
{
// ✅ Correct: The compiler generates the backing field automatically.
public string Name { get; set; }
}
Option B: Computed Property
If the property returns a calculated value (Read-Only).
public class User
{
// ✅ Correct: Has a 'get' accessor.
public string Name
{
get { return "Unknown"; }
}
// Or expression-bodied syntax (which effectively creates a getter):
// public string Name => "Unknown";
}
Option C: Use a Field
If you didn't mean to create a property at all, remove the braces.
public class User
{
// ✅ Correct: This is now a Field, not a Property.
public string Name;
}
Scenario 2: The Empty Indexer Block
Indexers allow you to access an object like an array (e.g., myObject[0]). Like properties, they require get or set accessors to function.
Example of error
public class DataStore
{
private string[] _data = new string[10];
// ⛔️ Error CS0548: Indexer must have at least one accessor.
public string this[int index]
{
// Missing logic here
}
}
Solution: Implement Get/Set
Define how the indexer interacts with your internal data.
public class DataStore
{
private string[] _data = new string[10];
// ✅ Correct: Defines read and write access
public string this[int index]
{
get { return _data[index]; }
set { _data[index] = value; }
}
}
Conclusion
CS0548 enforces the structure of properties.
- Check the Braces: If you see
{ }, you must put something inside. - Add
get; set;: This is the standard fix for storing data. - Remove Braces: If you just wanted a simple variable, delete the braces to make it a Field.