CS0103-ás fordítási hiba

Az "azonosító" név nem létezik az aktuális környezetben

Kísérlet történt olyan név használatára, amely nem létezik az osztályban, névtérben vagy hatókörben. Ellenőrizze a név helyesírását, és ellenőrizze az using irányelveket és a szerelvényhivatkozásokat, hogy a használni kívánt név elérhető-e.

Ez a hiba gyakran fordul elő, ha egy változót egy ciklusban vagy blokkban try if deklarál, majd egy kódblokkból vagy egy külön kódblokkból próbál hozzáférni, ahogyan az alábbi példában látható:

Feljegyzés

Ez a hiba akkor is megjelenhet, ha hiányzik a greater than szimbólum a lambda kifejezés operátorában => . További információ: lambdas kifejezés.

using System;

class MyClass1
{
    public static void Main()
    {
        try
        {
            // The following declaration is only available inside the try block.
            var 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);
        }
    }
}

A következő példa a hibát oldja meg:

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);
        }
    }
}