Skip to main content

How to Resolve Error "CS1014: A get or set accessor expected" in C#

The Compiler Error CS1014 is a syntax error specifically related to Property definitions. The message reads: "A get or set accessor expected".

In C#, a Property is not a method, nor is it a simple field. It is a special member that provides flexible mechanisms to read, write, or compute the value of a private field. Within the curly braces { } of a property definition, the compiler strictly expects one of three keywords: get, set, or init. If you try to write executable code (like a return statement or variable declaration) directly inside the property body without wrapping it in an accessor, the compiler raises CS1014.

This guide explains how to structure property syntax correctly.

Understanding Property Syntax

A property must contain specific blocks (accessors) to handle data.

  • get: Defines how to retrieve the value.
  • set / init: Defines how to assign the value.

You cannot place logic directly inside the property's main braces.

Valid Structure:

public int MyProperty
{
get { return 0; } // OK
set { } // OK
}

Invalid Structure:

public int MyProperty
{
return 0; // Error: Where is 'get'?
}

Scenario 1: Writing Logic Directly in the Body

This error is common among developers moving from languages where getters are defined implicitly, or when simply forgetting the keyword. You might try to write a return statement immediately inside the property.

Example of error

public class User
{
private string _name = "Alice";

public string Name
{
// ⛔️ Error CS1014: A get or set accessor expected.
// You cannot write code here. You must define WHICH operation this code belongs to.
return _name;
}
}

Solution: Add the 'get' Accessor

Wrap the logic inside a get block.

public class User
{
private string _name = "Alice";

public string Name
{
// ✅ Correct: The logic is now inside the getter.
get
{
return _name;
}

// Optional setter
set
{
_name = value;
}
}
}

Scenario 2: Malformed Expression-Bodied Members

C# supports "Expression-Bodied Members" using the => syntax. A common confusion arises when trying to combine the arrow syntax with the block syntax { }.

Example of error

Using the arrow => inside the curly braces without a get label.

public class Calculator
{
public int Value { get; set; }

public int DoubleValue
{
// ⛔️ Error CS1014: A get or set accessor expected.
// The compiler sees braces { ... } and expects keywords, not an arrow.
=> Value * 2;
}
}

Solution: Remove Braces or Add 'get'

You have two valid choices for fixing this syntax.

Option A: Pure Expression-Bodied Property (No Braces)

This acts as a read-only property.

public class Calculator
{
public int Value { get; set; }

// ✅ Correct: No braces, just the arrow. Implies 'get'.
public int DoubleValue => Value * 2;
}

Option B: Expression-Bodied Accessor (Inside Braces)

If you want to keep the braces (perhaps to add a setter later), you must explicitly use get =>.

public class Calculator
{
public int Value { get; set; }

public int DoubleValue
{
// ✅ Correct: Explicitly defining the getter with arrow syntax.
get => Value * 2;
}
}
note

Auto-Properties: If you intended a simple storage property, ensure you didn't accidentally type logic. A standard auto-property looks like this: public int MyProp { get; set; }

Conclusion

CS1014 is a structural error inside a property definition.

  1. Check the Block: Inside public Type Name { ... }, ensure you only see get, set, or init.
  2. Check Logic: If you see return ... or assignments, wrap them in get { ... } or set { ... }.
  3. Check Arrows: If using =>, either remove the outer curly braces or ensure you typed get =>.