Skip to main content

How to Resolve Error "CS1043: { or ; expected" in C#

The Compiler Error CS1043 is a syntax punctuation error. The message reads: "{ or ; expected".

This error occurs when the C# compiler is analyzing the structure of your code and encounters a token that disrupts the expected grammar. It essentially says: "At this specific point, you should have either started a new code block (using {) or finished the current statement (using ;), but you did neither."

This is most commonly found in Property Definitions (specifically auto-properties) and Do-While Loops.

Understanding Punctuation in C#

The C# compiler relies on punctuation to define the boundaries of logic:

  • { (Open Brace): Starts a body (Class, Method, Property, or Block).
  • ; (Semicolon): Ends a statement or declaration.

If you are declaring a property like public int X, the compiler waits to see if you will start a block { get; ... } or end the line ; (if it's a field). If you type something else, or forget a delimiter inside the block, CS1043 is raised.

Scenario 1: Malformed Property Accessors

This is the most frequent cause. When defining Auto-Implemented Properties, every accessor (get and set) must be immediately followed by a semicolon ;. If you forget one, the compiler gets confused about whether you meant to end the accessor or start a custom implementation body.

Example of error

Missing the semicolon after set.

public class User
{
// ⛔️ Error CS1043: { or ; expected
// The compiler sees 'set' and the closing '}'.
// It expects 'set;' (auto) or 'set { ... }' (manual).
public string Name { get; set }
}

Solution: Add Semicolon or Body

If you want an auto-property, add the semicolon.

public class User
{
// ✅ Correct: Both accessors end with a semicolon.
public string Name { get; set; }
}

If you intended to write a custom setter, add the braces.

public class User
{
private string _name;

public string Name
{
get { return _name; }
// ✅ Correct: Using a body block.
set { _name = value; }
}
}

Scenario 2: Missing Semicolon in do...while

The do...while loop is unique because it requires a semicolon at the very end of the line, after the condition. Unlike while or for loops (which do not use semicolons after their condition), the do loop is a statement that must be terminated.

Example of error

public void Process()
{
int i = 0;
do
{
i++;
} while (i < 10) // ⛔️ Error CS1043: { or ; expected

// The compiler doesn't realize the loop is done.
Console.WriteLine("Done");
}

Solution: Terminate the Loop

Add the semicolon after the closing parenthesis.

public void Process()
{
int i = 0;
do
{
i++;
} while (i < 10); // ✅ Correct

Console.WriteLine("Done");
}

Scenario 3: Variable Declaration Syntax

Sometimes this error appears when you try to declare multiple variables on one line but mess up the comma syntax, making it look like a new statement started without the previous one ending.

Example of error

Using spaces instead of commas to separate variables.

public void Math()
{
// ⛔️ Error CS1043: { or ; expected
// The compiler sees 'int x = 5'. It expects a ';' to end the line.
// Instead, it finds 'y', which confuses it.
int x = 5 y = 10;
}

Solution: Use Commas or Semicolons

Use commas for the same type, or semicolons for distinct statements.

public void Math()
{
// ✅ Correct: Same type list
int x = 5, y = 10;

// ✅ Correct: Separate statements
int a = 5;
int b = 10;
}

Conclusion

CS1043 is a sign that you left a statement hanging.

  1. Check Properties: Look inside { get; set; }. Ensure semicolons are present after both keywords.
  2. Check Do-While: Look at the end of while(condition). Is there a ;?
  3. Check Separators: Ensure variable lists are separated by commas ,, not spaces.