What a syntax error is
People make mistakes, and people write programs. To cope, a language tells you what went wrong. A syntax error is a mistake in how the code is written, like a grammar mistake in English. "Would you some tea?" is missing a verb, and a reader notices at once. The C# compiler is just as strict.
C# is a compiled language. Before your program runs at all, the compiler reads the whole source file. If it finds a syntax error, it reports it and nothing runs, not even the correct lines above the mistake. There is no partial output. You fix the code, compile again and try again.
Common syntax errors
A missing closing parenthesis
Console.WriteLine("Hello!";
Every ( needs a ). The compiler reports CS1026: ) expected and points at the spot where it expected the parenthesis.
A missing closing quote
Console.WriteLine("I love coding!);
The string starts at the first " and never ends, so the compiler reaches the end of the line still inside it. You get CS1010: Newline in constant, often followed by more messages about the missing ) and ;. Those extra messages are consequences of the first mistake, not separate bugs.
Mixed-up quotes
string greeting = 'Hey there!";
Single quotes start a char, which holds exactly one character. Strings use double quotes at both ends. This line mixes the two kinds and produces several errors, including Newline in constant.
A missing semicolon
Console.WriteLine("Hey")
Each statement ends with ;. Without it, the compiler reports CS1002: ; expected.
Another kind of error: names the compiler can't find
Sometimes the spelling is fine but the name means nothing. These are still compile errors, so the program doesn't run, but the compiler describes them differently:
Console.WrteLine("Hey");
The message is 'Console' does not contain a definition for 'WrteLine'. A misspelled variable is reported in a similar way:
string animal = "parrot";
Console.WriteLine(aniimal);
This gives The name 'aniimal' does not exist in the current context. Capital letters count too: console.WriteLine("Hey") fails with the same kind of message, because console is not Console.
How to read the message
- Look at the line and column. A message like
p1.cs(3,27)means line 3, column 27 of the file. Start there, and look at the line just before it too, because a problem on one line is sometimes only noticed on the next. - Read the code and the words.
CS1026is a stable code you can look up, and the words say what the compiler wanted. - Fix the first error first. A single missing quote or bracket can cause several errors after it. Fix the first one, compile again and see how many are left.
- Count pairs. Check that parentheses, braces and quotes each have a partner.
Don't confuse these with runtime errors. Those happen after a successful compile, while the program is running, for example dividing an int by zero, which throws a DivideByZeroException. This lesson covers only compile-time mistakes.
The exercise
The program has two lines that should print Hello! and I love coding!, but each has a small syntax error, so the compiler rejects it:
Console.WriteLine("Hello!";
Console.WriteLine("I love coding!);
- Line 1 is missing its closing parenthesis. The string is fine, so add
)between the closing quote and the semicolon. - Line 3 is missing the closing quote of its string. Add
"after the exclamation mark and before the).
Console.WriteLine("Hello!");
Console.WriteLine("I love coding!");
After the fix, the program compiles and prints the two lines:
Hello!
I love coding!
Key takeaways
- A syntax error means the code is not written according to C#'s rules. The compiler stops, and nothing runs.
- Typical causes are a missing
),"or;, and mixed single and double quotes. - A misspelled method or variable name is also a compile error, with a message about a name that doesn't exist.
- Read the first message, check its line, fix it and compile again.