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);
- Fix line 1. The method is
WriteLine, with thee. Correcting the spelling makes the first line valid. - Fix line 4. The variable is declared as
weatheron line 3, but line 4 useswhether, a real English word but a different name. Change it toweather.
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, notconsole. - Read the quoted name in the message, compare it with the declaration, and fix errors from the top down.