Start learning free

LESSON 6 OF 347 · WELCOME TO C#

Name Errors in C#: Unknown Names and Compiler Errors

Lesson Read the full explanation

TL ; DR:

  • A NameError occurs when C# sees a word it does not recognize. Some examples:

  • Misspelling a function name:

    Console.WrteLine("Heyooo!"); // NameError: name 'Console.WrteLine' is not defined
    

  • Misspelling a variable name:

    string animal = "parrot";
    Console.WriteLine(anmal); // NameError: name 'anmal' is not defined
    

  • When C# encounters a name error, it immediately stops executing your code and throws a NameError 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#

    Name Errors in C#, explained

    What a name error is

    Every word in a C# program is either part of the language, such as int or string, or a name you or the libraries defined: a variable, a method, a class. When you use a name the compiler can't find, you have a name error. The usual causes are a typo, wrong capitalisation, or using a variable that was never declared.

    In some languages an unknown name is only reported while the program runs, as a NameError. C# works differently. C# is a compiled language, so the compiler checks every name before the program starts. If any name is unknown, the compiler reports an error and nothing runs at all, not even the lines above the mistake. The messages carry codes like CS0103 and CS0117 instead.

    Syntax errors and name errors

    Two kinds of mistakes are common in the first lessons, and the compiler tells them apart.

    A syntax error means the code is not written according to the grammar of C#, like a sentence with a missing verb. The compiler can't make sense of the line at all:

    Console.WriteLine"Hey");
    

    The opening parenthesis is missing. The compiler complains that a ; is expected, because it stops reading the statement at WriteLine. A string that starts with " and ends with ' is the same kind of mistake: the string never closes, and you get Newline in constant.

    A name error means the grammar is fine, but a name doesn't exist. Two kinds appear constantly.

    A misspelled method name

    Console.WritLine("Welcome!");
    

    Console exists, but it has no member called WritLine. The compiler reports 'Console' does not contain a definition for 'WritLine'.

    A misspelled variable name

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

    The variable is called animal, but the second line asks for anmal. The message is The name 'anmal' does not exist in the current context.

    Case matters

    C# is case-sensitive. console.WriteLine("Hi") fails because the class is Console, and Writeline is not WriteLine. The same goes for variables: weather and Weather are two different names. If you declare one and use the other, the compiler reports the name as missing.

    Reading the compiler's message

    Each message points at a line and column and quotes the name it couldn't resolve. Read the quoted name first and compare it letter by letter with the declaration or the documentation. Fix the first error you see, then compile again: one early typo often causes several errors further down, and they vanish together.

    Also check scope. A name can be spelled correctly and still not exist at that point, for example when a variable is declared inside a block between braces and you use it after the closing brace.

    The exercise, step by step

    The starter program should print Welcome! and then sunny, but it has two unknown names:

    Console.WritLine("Welcome!");
    
    string weather = "sunny";
    Console.WriteLine(whether);
    
    1. Fix line 1. The method is WriteLine, with the e. Correcting the spelling makes the first line valid.
    2. Fix line 4. The variable is declared as weather on line 3, but line 4 uses whether, a real English word but a different name. Change it to weather.

    The fixed program:

    Console.WriteLine("Welcome!");
    
    string weather = "sunny";
    Console.WriteLine(weather);
    

    Both errors had to be fixed before anything would print. Fixing only the first leaves the second one, and the compiler still refuses to run the program. When it finally compiles, the output is:

    Welcome!
    sunny
    

    Key takeaways

    • A name error means the compiler can't find a method, class or variable with that exact spelling.
    • C# reports it before the program runs, so no output appears until every error is fixed.
    • A syntax error breaks the grammar of a line; a name error uses valid grammar with an unknown word.
    • Names are case-sensitive: Console, not console.
    • Read the quoted name in the message, compare it with the declaration, and fix errors from the top down.
    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