次の方法で共有


コンパイラ エラー CS0269

未割り当ての out パラメーター 'parameter' が使用されました

コンパイラは、使用前に out パラメーターに値が割り当てられたことを確認できなかった可能性があります。その値は、割り当てられたときに未定義だった可能性があります。 値にアクセスする前に、呼び出されるメソッドの out パラメーターに必ず値を割り当てます。 渡された変数の値を使用する必要がある場合、代わりに ref パラメーターを使用します。 詳細については、「メソッド パラメーター」を参照してください。

例 1

次の例では CS0269 が生成されます。

// CS0269.cs  
class C  
{  
    public static void F(out int i)  
    // One way to resolve the error is to use a ref parameter instead  
    // of an out parameter.  
    //public static void F(ref int i)  
    {  
        // The following line causes a compiler error because no value  
        // has been assigned to i.  
        int k = i;  // CS0269  
        i = 1;  
        // The error does not occur if the order of the two previous
        // lines is reversed.  
    }  
  
    public static void Main()  
    {  
        int myInt = 1;  
        F(out myInt);  
        // If the declaration of method F is changed to require a ref  
        // parameter, ref must be specified in the call as well.  
        //F(ref myInt);  
    }  
}  

例 2

これは、変数の初期化が try ブロックで発生する場合にも発生することがあります。コンパイラは成功を確認できません。

// CS0269b.cs  
class C  
{  
    public static void F(out int i)  
    {  
        try  
        {  
            // Assignment occurs, but compiler can't verify it  
            i = 1;  
        }  
        catch  
        {  
        }  
  
        int k = i;  // CS0269  
        i = 1;  
    }  
  
    public static void Main()  
    {  
        int myInt;  
        F(out myInt);  
    }  
}