How to Resolve Error "CS1002: ; expected" in C#
The Compiler Error CS1002 is one of the most frequent syntax errors in C#. The message reads: "; expected".
C# is a C-style language, which means it uses the semicolon ; as a Statement Terminator. Just as a period . marks the end of a sentence in English, a semicolon marks the end of a logical instruction in C#. If you omit it, the compiler doesn't know where one instruction ends and the next begins, causing it to crash the parsing process.
This guide covers the most common places where semicolons are forgotten and how to fix them.
Understanding the Semicolon Rule
In C#, whitespace (spaces, newlines, tabs) is mostly ignored. You could write an entire program on a single line if you wanted to. Because newlines don't end commands, the semicolon is strictly required to signal the end of:
- Variable declarations (
int x = 5;) - Method calls (
Console.WriteLine();) return,break,continuestatements.do...whileloops.
Exceptions (No Semicolon):
- After block closing braces
}(for methods, classes,if,for,while). - After a class declaration (though allowed, it's not required).
Scenario 1: End of Statements (Variables and Calls)
This is the classic "muscle memory" error. You finish typing a thought but forget to type the terminator character.
Example of error
public class Program
{
public void Run()
{
// ⛔️ Error CS1002: ; expected
// The compiler sees "int x = 10 Console.WriteLine..." and gets confused.
int x = 10
Console.WriteLine(x) // ⛔️ Error CS1002 here too
}
}
Solution: Add the Semicolon
Terminate every instruction.
public class Program
{
public void Run()
{
// ✅ Correct: Statement ended.
int x = 10;
// ✅ Correct: Method call ended.
Console.WriteLine(x);
}
}
Scenario 2: The do-while Loop
The do-while loop is unique. Standard while loops and for loops do not end with a semicolon because they are followed by a code block {}. However, do-while puts the condition at the end. Because the statement physically ends after the condition, a semicolon is required.
Example of error
public void ProcessLoop()
{
int i = 0;
do
{
i++;
} while (i < 5) // ⛔️ Error CS1002: ; expected
// The compiler expects the statement to finish here.
}
Solution: Terminate the Loop
Add the semicolon after the closing parenthesis of the while condition.
public void ProcessLoop()
{
int i = 0;
do
{
i++;
} while (i < 5); // ✅ Correct: Required for do-while loops
}
Scenario 3: Return Statements and Logic
Sometimes semicolons are missed inside complex logic or immediately after a return keyword if the developer is editing quickly.
Example of error
public int Add(int a, int b)
{
// ⛔️ Error CS1002: ; expected
return a + b
}
Solution: Terminate the Return
public int Add(int a, int b)
{
// ✅ Correct
return a + b;
}
The "Empty Statement":
Writing ;; (two semicolons) is valid. The second one is treated as an empty statement that does nothing. While not an error, it is unnecessary code.
Conclusion
CS1002 is the compiler asking you to finish your sentence.
- Check Previous Lines: Often, the error points to line X, but the missing semicolon is actually on line X-1.
- Check Variable Definitions:
var x = 1; - Check Method Calls:
Method(); - Check Do-While:
do { } while(condition);