How to Resolve Error "CS1007: Property accessor already defined" in C#
The Compiler Error CS1007 is a syntax error regarding Properties. The message reads: "Property accessor already defined".
In C#, a property can encapsulate data access using specific keywords: get, set, and (in C# 9+) init. The rule is simple: a property can have at most one of each type of accessor. You cannot define two getters or two setters for the same property. This error occurs when the compiler finds a duplicate keyword within the property's code block.
This guide explains how these duplications occur and how to clean up your property definitions.
Understanding Property Accessors
A standard property looks like this:
public int MyProperty
{
get { return _myField; } // Reads data
set { _myField = value; } // Writes data
}
The compiler uses the keywords get and set to generate methods (get_MyProperty and set_MyProperty). If you define get twice, the compiler tries to generate two methods with the exact same name, which is impossible.
Scenario 1: Duplicate Keywords (Copy-Paste Errors)
This is the most common cause. When manually implementing a property (expanding it from an auto-property), it is easy to copy the get block to create the set block but forget to change the keyword to set.
Example of error
Here, the developer intended to write a setter, but accidentally wrote get a second time.
public class User
{
private string _name;
public string Name
{
// First Getter
get { return _name; }
// ⛔️ Error CS1007: Property accessor already defined.
// You cannot have a second 'get'. This should probably be 'set'.
get { _name = value; }
}
}
Solution
Identify the duplicate block and rename the keyword to set (or remove the block if it was accidental).
public class User
{
private string _name;
public string Name
{
// ✅ Correct: One getter, one setter.
get { return _name; }
set { _name = value; }
}
}
Scenario 2: Mixing Expression-Bodied and Block Syntax
C# allows "Expression-Bodied Members" (=>) for concise syntax. However, you cannot define an accessor using the arrow syntax and also define it using the block syntax { } in the same property.
Example of error
Trying to use both styles simultaneously for the same accessor.
public class ScoreKeeper
{
private int _score;
public int Score
{
// Expression-bodied getter
get => _score;
// ⛔️ Error CS1007: Property accessor already defined.
// The compiler sees this as a second getter definition.
get { return _score; }
set { _score = value; }
}
}
Solution
Choose one style and stick to it for that specific accessor.
public class ScoreKeeper
{
private int _score;
public int Score
{
// ✅ Correct: Only one definition for 'get'.
get => _score;
set => _score = value;
}
}
Scenario 3: Conflict Between set and init
Introduced in C# 9.0, the init accessor is a specialized version of set that only allows assignment during object initialization. Since init is technically a setter (with extra rules), you cannot have both set and init on the same property. They are mutually exclusive.
Example of error
public class Configuration
{
public string Environment
{
get;
// Standard Setter
set;
// ⛔️ Error CS1007: Property accessor already defined.
// 'init' counts as a setter. You must choose one or the other.
init;
}
}
Solution
Decide on the mutability of your property.
- Use
setif the value can change at any time. - Use
initif the value should be immutable after creation.
public class Configuration
{
public string Environment
{
get;
// ✅ Correct: Chose 'init' for immutability.
init;
}
}
Conclusion
CS1007 ensures that a property has a clear, unambiguous definition.
- Check for Typos: Look for two
getkeywords or twosetkeywords. - Check Syntax: Ensure you aren't defining the same accessor twice using different syntax styles (
=>vs{ }). - Check
init: Remember thatinitreplacesset; you cannot have both.