Compiler error · error CS1022
Type or namespace definition, or end-of-file expected
That message is compiler-speak for: you have one closing brace too many. There's a } with no block left to close.
See it happen
int x = 5;
Console.WriteLine(x);
} // ← nothing was opened for this brace to close
It's the mirror of CS1513
| Error | Meaning | Fix |
|---|---|---|
CS1513 — } expected | Too few closing braces | Add the missing } |
| CS1022 | Too many closing braces | Delete the extra } |
Same diagnostic method, opposite direction. Count from the top of the file: { is +1, } is −1. If the count goes negative, you've just passed the extra brace.
The usual causes
1. Copy-paste brought an extra brace
class Greeter
{
public void Hello()
{
Console.WriteLine("hi");
}
} // closes class
} // ← pasted twice by accident
2. A block was deleted but its brace stayed
You removed an if or a method, and left its closing brace behind. The symptom is often a confusing cascade: everything after it appears to be outside the class.
3. Braces merged while reformatting
Auto-format can't fix broken brace counts — it can only align what's there. If two blocks merged, reindent visually and look for a line whose indentation doesn't match its neighbours.
How to fix it in 20 seconds
- Click the first
{in the file; most editors highlight its partner. - If a
}highlights nothing, that brace has no opening partner — delete it. - Or count +1/−1 from the top; the first place it dips negative is your extra brace.
- After deleting, run the auto-formatter — if the layout suddenly snaps into place, you found it.
This works in Visual Studio, VS Code and Rider. The engine here reports the same code the real compiler does, so you can practice the pattern above.
Frequently asked
What does CS1022 mean?
One more } than { — an extra closing brace. Count +1/−1 from the top; where the count goes negative is the brace to remove.
CS1022 vs CS1513?
CS1022 = too many closing braces; CS1513 = too few. Same counting method, opposite direction.
Checklist
- Count
{(+1) and}(−1) from the top of the file. - First negative point = the extra brace.
- Did you paste code? Check the end of the pasted region.
- Did you delete a block? Check whether its brace survived.
- Auto-format after the fix to confirm the structure is sane.