Compiler Error CS0103

The name 'identifier' does not exist in the current context

An attempt was made to use a name that does not exist in the class, namespace, or scope. Check the spelling of the name and check your using statements and assembly references to make sure that the name you are trying to use is available.

This error will occur if you declare a variable within a loop or a try or if block and then attempt to access it from an enclosing code block or another code block, as shown in the following example.

The following sample generates CS0103:

// CS0103.cs
using System;

class MyClass
{
    public static void Main()
    {
        // To resolve the error in this example, the first step is to uncomment 
        // the following line, moving the declaration of conn out of the try block.
        //MyClass conn = null;
        try
        {
            // The second step in resolving this error is to remove or comment
            // out the following line of code.
            MyClass conn = new MyClass();   
            // The third step in the resolution is to uncomment the following line.
            // conn = new MyClass();
        }
        catch (Exception e)
        {
            // Variable conn only exists in the try block.
            if (conn != null)   // CS0103
                Console.WriteLine("{0}", e);
        }
    }
}

Change History

Date

History

Reason

July 2009

Expanded the explanation of the example.

Customer feedback.