Start learning free

LESSON 36 OF 347 · FUNCTIONS I

Fixing a Non-Existing Variable Error in C#

Practice Read the full explanation

Inside the code editor we've tried to define and call a function that takes a number as argument and prints the double of that number to the console.

So when we ran the code, we expected it to print:

20

but it seems like we made some mistakes because when we run our code we get an error instead.


Assignment:

Your task is to fix our code such that no errors will be produced and it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    C# · FUNCTIONS I

    Buggy Code Non Existing Variable in C#, explained

    What the code should do

    The program defines a method, PrintDouble, that takes one number and prints twice its value. MainFunction calls it with 10, so the expected output is 20. Instead of output you get a compiler error, because the code never builds.

    Here is the code you start with:

    public void PrintDouble(int n) {
        Console.WriteLine(2 * num);
    }
    
    public void MainFunction() {
        PrintDouble(10);
    }
    

    What goes wrong

    The compiler reports this message for the line inside PrintDouble:

    error CS0103: The name 'num' does not exist in the current context
    

    The method header declares a parameter called n. The body, however, uses a name that was never declared: num. To the compiler, n and num are two unrelated names. It looks for num among the method's parameters, the variables declared so far inside the method, and the members of the class. It finds nothing, so it stops with an error.

    The mistake is easy to make because a person reading the code sees "the number" in both places. The compiler only compares spelling, character by character.

    Parameters only exist inside their method

    A parameter is a variable that the method creates for each call. Its name is whatever the header says, and the body must use exactly that name. When MainFunction runs PrintDouble(10), the value 10 is stored in n. Nothing is ever stored in anything called num, because no such variable exists.

    The same rule covers every kind of variable. A name is only usable where it has been declared, which is called its scope. A parameter's scope is the method's body, and a local variable's scope runs from its declaration to the end of the block that holds it. The "does not exist in the current context" message is how C# tells you that the name you wrote is not in scope.

    C# is also case-sensitive. n and N are different names, and so are count and Count. A wrong capital letter produces the same error as a misspelling.

    Fixing it step by step

    1. Read the error. It names the identifier it could not find, num, and the line where it appears.
    2. Look at the declaration. The header of PrintDouble says int n, so the available name is n.
    3. Make the two match. Change the body to use n. The call in MainFunction is already correct and stays as it is.

    The fixed code in C#

    public void PrintDouble(int n) {
        Console.WriteLine(2 * n);
    }
    
    public void MainFunction() {
        PrintDouble(10);
    }
    
    /* Explanation:
    
    We got an error because inside the original function we used a variable "num", which is not the parameter "n", so it wasn't recognized.
    
    One fix is to use "n" instead of "num" in the line "Console.WriteLine(2 * n);"
    
    Another possible fix is to change the parameter name to "num", like this:
    public void PrintDouble(int num) {
        Console.WriteLine(2 * num);
    }
    */
    

    The comment at the end is the lesson's own explanation. It is ignored when the program is compiled. Now 2 * n is 2 * 10, and Console.WriteLine prints 20 followed by a line break. The multiplication produces an int, and WriteLine converts it to text for you.

    The other fix

    You could instead rename the parameter, so the header reads PrintDouble(int num) and the body keeps 2 * num. That also compiles and prints 20. Either way, the rule is the same: the name in the header and the name in the body must be identical. Choose whichever makes the code easier to read. A short name like n is fine for a one-line method, and a longer one like num or number helps when the body grows.

    Common mistakes

    • Typos. nmu for num, or Console.WriteLine(2 * m) for n. The compiler treats any undeclared spelling as a new, missing name.
    • Wrong capitalization. A parameter called name cannot be used as Name in the body.
    • Using a variable outside its scope. A variable declared inside one method is invisible to another. If MainFunction printed n, it would get the same error.
    • Fixing the call instead of the body. Changing PrintDouble(10) does not help. The call supplies the value and the error is about the name inside the method.
    • Using a variable before declaring it. Inside a method, a local variable cannot be used on a line above its declaration.

    Why the compiler is strict about this

    Some languages let you use a name that was never declared and only fail when that line runs. C# checks every name before the program starts. That is why you see this error as soon as you build, never halfway through a run, and why a method with a misspelled variable never executes at all. The strictness is useful: a misspelled name is caught immediately, on the exact line, and not hidden until some input happens to reach it.

    Checking your fix

    Run the code in the editor above. A correct fix compiles without errors and prints 20. If a different error appears, read it the same way: it names the line and the identifier. The compiler reports every error it finds, so fix them one at a time, starting with the first, and rebuild after each fix.

    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