Start learning free

LESSON 6 OF 674 · WELCOME TO C#

Name Errors in C#

Lesson

TL ; DR:

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

  • Misspelling a function name:

    Console.WrteLine("Heyooo!"); // error CS0117: 'Console' does not contain a definition for 'WrteLine'
    

  • Misspelling a variable name:

    string animal = "parrot";
    Console.WriteLine(anmal); // error CS0103: The name 'anmal' does not exist in the current context
    

  • When C# encounters a name error, the compiler refuses to build your program, so none of your code runs, and it reports an error like CS0103 or CS0117 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 CS1010: Newline in constant.

2. Leaving out a parantheses:

Console.WriteLine"Hey");

When we run this code we'll get an error CS1002: ; expected.


The reference error:

A reference error occurs when the C# compiler 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 CS0117: 'Console' does not contain a definition for 'WrteLine'.

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 CS0103: The name 'aniimal' does not exist in the current context.


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