Compilerfehler CS0103

Der Name "Bezeichner" ist im aktuellen Kontext nicht vorhanden

Es wurde versucht, einen Namen zu verwenden, der in der Klasse, im Namespace oder im Bereich nicht vorhanden ist.Überprüfen Sie die Schreibweise des Namens und überprüfen Sie using Directive und die Assemblyverweise, um zu überprüfen, ob der Name, den Sie verwenden möchten, verfügbar ist.

Dieser Fehler tritt häufig auf, wenn Sie eine Variable in einer Schleife oder in einem try oder if-Blocks deklarieren und anschließend versuchen, sie von einem einschließenden Codeblock oder in einem separaten Codeblock zugreifen, wie im folgenden Beispiel gezeigt.

using System;

class MyClass1
{
    public static void Main()
    {
        try
        {
            // The following declaration is only available inside the try block.
            MyClass1 conn = new MyClass1();
        }
        catch (Exception e)
        {
            // The following expression causes error CS0103, because variable
            // conn only exists in the try block.
            if (conn != null) 
                Console.WriteLine("{0}", e);
        }
    }
}

Im folgenden Beispiel löst den Fehler auf.

using System;

class MyClass2
{
    public static void Main()
    {
        // To resolve the error in the example, the first step is to 
        // move the declaration of conn out of the try block. The following
        // declaration is available throughout the Main method.
        MyClass2 conn = null;
        try
        {
            // Inside the try block, use the conn variable that you declared
            // previously.
            conn = new MyClass2();
        }
        catch (Exception e)
        {
            // The following expression no longer causes an error, because
            // the declaration of conn is in scope.
            if (conn != null) 
                Console.WriteLine("{0}", e);
        }
    }
}