コンパイラ エラー CS0103

名前 'identifier' は現在のコンテキスト内に存在していません

クラス、名前空間、スコープに存在しない名前を使用しようとしました。 名前のスペルを確認し、使用中のディレクティブとアセンブリの参照を確認して、使用しようとしている名前が利用できることを確認します。

このエラーは、ループあるいは try または if ブロックに変数を宣言し、外側のコード ブロックまたは別のコード ブロックから変数にアクセスしようとするときに頻繁に発生します。次の例をご覧ください。

Note

このエラーは、式ラムダの演算子 =>greater than 記号がないときにも発生することがあります。 詳細については、「式ラムダ」を参照してください。

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

次の例では、エラーを解決しています。

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