How to Resolve Error "CS0547: property or indexer cannot have void type" in C#
The Compiler Error CS0547 is a syntax and semantic error. The message reads: " 'PropertyName' : property or indexer cannot have void type".
In C#, a Property is designed to represent data—it allows you to read ("get") or write ("set") a value. Because a property conceptually represents a value (like a variable), it must have a valid data type (like int, string, or a custom class) that defines what kind of data passes through it. void means "no data," which creates a logical contradiction: you cannot "get" nothing, nor can you assign a value to "nothing".
This guide explains why properties require types and how to refactor your code if you intended to perform an action instead.
Understanding Property Semantics
- Variables/Fields: Store data. Must have a type.
- Properties: Encapsulate fields. Must have the same type as the data they represent.
- Methods: Perform actions. Can return
void(no result).
If you declare public void MyProp { get; set; }, the compiler asks: "When someone writes var x = obj.MyProp, what is stored in x?" Since void cannot be stored, the definition is invalid.
Scenario 1: Defining a Void Property
This error usually happens when a developer creates a "Write-Only" property intending to trigger an action (like logging or resetting state) without returning any value, so they instinctively type void.
Example of error:
public class Logger
{
// ⛔️ Error CS0547: 'Logger.LogEntry' : property or indexer cannot have void type.
// Properties must represent a value, even if they are write-only.
public void LogEntry
{
set { System.Console.WriteLine(value); }
}
}
Solution 1: Assign a Specific Data Type
If the property is meant to accept data (via the set accessor), you must declare the property with the type of data it accepts.
Solution: change void to the type of the value keyword used in the setter.
public class Logger
{
// ✅ Correct: The property accepts a 'string'.
// Even without a getter, the type defines what can be assigned to it.
public string LogEntry
{
set { System.Console.WriteLine(value); }
}
}
// Usage:
// logger.LogEntry = "Message";
Solution 2: Convert to a Method (Action)
In C# design guidelines, properties should generally represent state, not actions. If your property logic performs a complex operation, has side effects, or "does something" rather than "setting a value," it should be a Method. Methods naturally support void.
Solution: refactor the property into a method.
public class Logger
{
// ✅ Correct: Methods are designed for actions and can return void.
public void LogEntry(string message)
{
System.Console.WriteLine(message);
}
}
// Usage:
// logger.LogEntry("Message");
Conclusion
CS0547 enforces the distinction between data and behavior.
- Check the Intent: Does this member represent a piece of data (Name, ID, Color)?
- Yes: Give it a Type (
string,int,Color).
- Yes: Give it a Type (
- Does it perform an action? (Reset, Log, Calculate)?
- Yes: Change it from a Property to a Method (
void DoWork()).
- Yes: Change it from a Property to a Method (