Compiler error · error CS1513

} expected

A block was opened with { and never closed. The line number in the error is where the compiler ran out of code — usually the last line of the file — not where the missing } belongs.

See it happen

int x = 5;
if (x > 2)
{
    Console.WriteLine("big");
    if (x > 4)
    {
        Console.WriteLine("huge");
}

Two blocks opened, one closed. The compiler reads to the end of the file, realizes the outer if's block is still open, and reports } expected at the last line — far from the real crime scene.

int x = 5;
if (x > 2)
{
    Console.WriteLine("big");
    if (x > 4)
    {
        Console.WriteLine("huge");
    }
}

The brace-counting ritual (30 seconds)

  1. Put your cursor at the top of the function or block that's broken.
  2. Count downward: +1 for every {, −1 for every }.
  3. The count should return to 0 at the end. Where it "should" hit 0 but doesn't — the missing brace belongs at (or just before) that spot.

In Visual Studio / VS Code: click any brace and its partner highlights. If a { highlights nothing, you've found the orphan.

The three usual causes

1. Deleted a line, deleted its brace with it

You removed an if or a method call but left one of its braces behind — now some OTHER block closes too early, and everything after it is mis-nested. Symptom: weird CS0103 errors on variables you know exist, plus a CS1513 at the file's end.

2. Pasted code, lost the tail

Copy-paste from a tutorial grabbed everything except the final } — or grabbed one brace too many, which produces the mirror error CS1022 ("Extra } in type or namespace") instead.

3. Wrote the code, closed nothing

The honest one — long nested logic written top to bottom. The fix that scales: always type both braces the moment you type the header, then fill the middle:

if (x > 2)
{
    // fill in the middle AFTER both braces exist
}

Frequently asked

What does error CS1513 mean in C#?

Click any brace in Visual Studio or VS Code and its partner highlights; re-run the auto-formatter (Ctrl+K, D) so indentation is honest — misaligned indentation is how missing braces hide.

How do I find a missing brace quickly?

undefined

Why editors auto-close braces

That mildly annoying thing where the editor inserts } when you type {? It exists precisely to prevent CS1513. Keep it on, and let auto-format (Ctrl+K, D in VS) re-indent — misaligned indentation is how missing braces hide. Once every block lines up, the orphan brace is visually obvious.

Checklist