Start learning free

LESSON 5 OF 347 · WELCOME TO C#

Syntax Errors in C#: Reading Compiler Messages

Lesson Read the full explanation

TL ; DR:

  • Syntax errors are mistakes in the use of the C# language, analogous to spelling or grammar mistakes in a language like English. For example:


  • Omitting quotes when working with strings:

    Console.WriteLine("Welcome!);
    

  • Omitting parantheses when using the Console.WriteLine function:

    Console.WriteLine("Let's code!";
    

  • When C# encounters a syntax error, it immediately stops executing your code and throws a SyntaxError instead.




Full lesson:

Humans are prone to making mistakes. Humans are also typically in charge of creating computer programs. To compensate, programming languages attempt to understand and explain mistakes made in their programs.

Here are two common errors that we encounter while writing C# code:


The syntax error:

Syntax errors are mistakes in the use of the C# language, and are analogous to spelling or grammar mistakes in a language like English. For example, the sentence "Would you some tea?" does not make sense – it is missing a verb.

Syntax error means there is something wrong with the way your program is written. Some examples:

1. Misuse of commas when declaring a string:

string greeting = 'Hey there!";

When we run this code we'll get an error: unclosed character literal.

2. Leaving out a parantheses:

Console.WriteLine"Hey");

When we run this code we'll get an error: not a statement.


The reference error:

A reference error occurs when the C# interpreter sees a word it does not recognize. Some examples:

1. Misspelling a class / function name:

Console.WrteLine("Hey");

When we run this code we'll get an error: cannot find symbol: method prntln(string).

2. Using a non-existent variable / Misspelling a variable's name:

string animal = "parrot";
Console.WriteLine(aniimal);

When we run this code we'll get an error: cannot find symbol: variable aniimal.


Assignment
Follow the Coding Tutorial and let's practice with errors!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · WELCOME TO C#

    Syntax Errors in C#, explained

    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. CS1026 is 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!);
    
    1. Line 1 is missing its closing parenthesis. The string is fine, so add ) between the closing quote and the semicolon.
    2. 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.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free