Compile errors
In the first lesson, the compiler was a strict proofreader: it reads the whole recipe and lets no text with an error into the kitchen. Now let's see what its notes in the margin look like and how to read them.
The first line of this program is missing its semicolon:
Console.WriteLine("Hello") // ✗
Console.WriteLine("Bye");error CS1002: ; expectedThis is a compile error: the compiler found a broken language rule and refused to translate the text. The program did not run, so nothing was printed—not Hello, and not even Bye, although the second line is correct. The proofreader returned the whole recipe, and the kitchen did not carry out a single step.
The compiler's message
The full message for this error looks like this: hello.cs(1,27): error CS1002: ; expected. Instead of hello.cs, the compiler prints the full path to the file, and here only the name is kept to save space.
Let's take the message apart, from left to right:
hello.csis the file where the error was found.(1,27)is the line number and the character number in that line. On the first line, character 27 comes right after the closing parenthesis: that is where the compiler expected the semicolon.errormeans this is an error, and the program will not run.CS1002is the error code. With it, you can look the error up in the Microsoft documentation.; expectedsays what is broken: a semicolon was expected.
The course shows compiler messages in English. On a computer set to Russian, .NET may print them in Russian, for example error CS1002: Требуется ";"., but the error code does not change: messages are matched by it.
If there are several errors, the compiler lists them one per line. Fix them from top to bottom: later errors are often caused by the first one and go away with it. Below the list of errors, the dotnet run command asks you to fix them and run the program again.
Letter case
C# tells capital letters from small ones. To it, Console, console and CONSOLE are three different names, and it knows only the first one. So a single wrong letter is already a compile error:
Console.WriteLine("Start");
Console.Writeline("Finish"); // ✗error CS0117: 'Console' does not contain a definition for 'Writeline'The compiler reports that Console has no action called Writeline: there is only WriteLine with a capital L. If you write console instead of Console, the error is a different one, CS0103: the compiler does not know that name at all. In both cases the program does not run, and Start is not printed either.
Errors and warnings
Besides errors, the C# compiler reports warnings. Errors and warnings differ in what happens to the program:
- an error, a line with the word
error, stops the compilation, and the program does not run; - a warning, a line with the word
warning, does not stop the program: it compiles and runs, but the compiler has marked a suspicious place.
A warning is like the proofreader's "please check" in the margin: the recipe goes to the kitchen, but it may hold a mistake in meaning. That is why warnings are read and fixed too, and every example in the course compiles without them.
Common mistake
The closing quote is forgotten:
Console.WriteLine("Hello); // ✗
Console.WriteLine("Bye");error CS1010: Newline in constant
error CS1003: Syntax error, ',' expected
error CS1026: ) expectedThere is one mistake but three messages. Let's see where they came from:
- The literal starts with the quote before
Helloand is not closed before the end of the line. The first error says so: a line break appeared inside the literal. - The parenthesis, the semicolon and even
// ✗ended up inside the literal, so the call toWriteLineis not closed. - The compiler decided that the call goes on in the second line. It expects a comma before the next value (
CS1003) and a closing parenthesis (CS1026).
Add the quote after Hello, and the other two errors go away together with the first one.
Key points
- A compile error stops the program before it runs: not a single line runs, not even a correct one.
- The message names the file, the line and the character, the error code and what is broken:
hello.cs(1,27): error CS1002: ; expected. - C# tells capital letters from small ones:
Writelineinstead ofWriteLineandconsoleinstead ofConsoleare compile errors. - Errors are fixed from top to bottom; a warning does not prevent running, but it is read too.