Out パラメーターと C# の例

は、引数を参照型としてメソッドに渡すために使用される C# のキーワードです。通常、メソッドが複数の値を返す場合に使用されます。

重要な点:

  • ref キーワードと似ています。しかし、メインは リファレンスとアウトの違い キーワードは、ref がメソッドに渡す前に変数を初期化する必要があることです。ただし、out パラメーターでは、メソッドに渡す前に変数を初期化する必要はありません。ただし、呼び出し側メソッドに値を返す前に、呼び出されたメソッドで変数を初期化する必要があります。
  • これも in キーワードと似ていますが、 キーワードでは、呼び出したメソッドが引数値を変更することはできませんが、 参照 許可します。
  • 使用する場合 メソッド定義と呼び出しメソッドの両方でパラメータとしてキーワードを使用する必要があります。 キーワードを明示的に指定します。
  • out パラメーターは、非同期メソッドでは使用できません。
  • out パラメーターはイテレーター メソッドで使用することはできません。
  • メソッドには複数の out パラメータを含めることができます。
  • メソッド呼び出し時にoutパラメータをインラインで宣言できます。ただし、インライン出力パラメーターは、呼び出し先と同じコード ブロック内でアクセスできます。
  • メソッドのオーバーロードは、out パラメーターを使用して行うこともできます。
  • プロパティは変数ではないため、出力パラメータとして渡すことはできません。
  • C# 6.0 までは、ユーザーは最初に変数を宣言し、その後、出力引数としてのみ渡すことができます。ただし、C# 7.0 以降では、個別の変数宣言を除いて、ユーザーはメソッド呼び出しの引数リストで out 変数を宣言することもできます。

outパラメータの宣言:

// No need to initialize // the variable here data_type variable_name; Method_Name(out variable_name); // you can also convert both above two // lines of codes as follows from // C# 7.0 onwards Method_Name(out data_type variable_name); 

ここでの値は、 変数名 値を返す前に、呼び出されたメソッド内で初期化する必要があります。

例:

C#




// C# program to illustrate the> // concept of out parameter> using> System;> class> GFG {> > // Main method> > static> public> void> Main()> > {> > // Declaring variable> > // without assigning value> > int> i;> > // Pass variable i to the method> > // using out keyword> > Addition(> out> i);> > // Display the value i> > Console.WriteLine(> 'The addition of the value is: {0}'> , i);> > }> > // Method in which out parameter is passed> > // and this method returns the value of> > // the passed parameter> > public> static> void> Addition(> out> int> i)> > {> > i = 30;> > i += i;> > }> }>

出力:

The addition of the value is: 60 

複数の出力パラメータ: C# では、ユーザーは複数の out パラメータをメソッドに渡すことができ、メソッドは複数の値を返します。

例: 以下のコードでは、初期化せずに 2 つの値変数 (int i, j;) を宣言しました。次に、Addition(out i, out j); のような out キーワードを使用して、これらのパラメーターを Addition メソッドに渡します。これらの変数の値は、それらが渡されたメソッド内で割り当てられます。

C#




// C# program to illustrate the> // concept of multiple out parameter> using> System;> class> GFG {> > // Main method> > static> public> void> Main()> > {> > // Declaring variables> > // without assigning values> > int> i, j;> > // Pass multiple variable to> > // the method using out keyword> > Addition(> out> i,> out> j);> > // Display the value i and j> > Console.WriteLine(> 'The addition of the value is: {0}'> , i);> > Console.WriteLine(> 'The addition of the value is: {0}'> , j);> > }> > // Method in which out parameters> > // are passed and this method returns> > // the values of the passed parameters> > public> static> void> Addition(> out> int> p,> out> int> q)> > {> > p = 30;> > q = 40;> > p += p;> > q += q;> > }> }>

出力:

The addition of the value is: 60 The addition of the value is: 80 

C# 7.0 での Out パラメーターの強化: C# 7.0 では、out パラメーターにいくつかの新機能が追加されています。その機能は次のとおりです。

  • C# 7.0 では、out パラメーターは宣言と初期化なしで渡すことができます。 Outパラメータのインライン宣言 または暗黙的 パラメータを入力します 。そのスコープはメソッド本体、つまりローカル スコープに限定されます。
  • out パラメータの使用が許可されています だった メソッドパラメータリストに入力します。
  • in out パラメータでは、out パラメータの名前が定義と呼び出しの両方で同じである必要はありません。
  • Try Patternでも使用できます。

例: 以下のプログラムは、Out パラメータのインライン宣言を示しています。ここでのコード行、つまり Area(out int length, out int width, out int Rarea);これらの変数はメソッド呼び出し内で直接宣言されるため、Out パラメーターのインライン宣言が含まれます。変数の値は、変数が渡されたメソッド内で初期化されます。

注記: この例を実行するには、C# 7.0 バージョンが必要です。

例:

C#




// C# program to illustrate the> // concept of out parameter> using> System;> class> GFG> {> > // Main method> > static> public> void> Main()> > {> > // In-line declaring variables> > // without assigning values> > // Passing multiple variable to> > // the method using out keyword> > Area(> out> int> length,> out> int> width,> out> int> Rarea);> > // Display the value length, width, and Rarea> > System.Console.WriteLine(> 'Length of the rectangle is: '> + length);> > System.Console.WriteLine(> 'Width of the rectangle is: '> + width);> > System.Console.WriteLine(> 'Area of the rectangle is: '> + Rarea);> > Console.ReadLine();> > }> > // Method in which out parameters are passed> > // and this method returns the values of> > // the passed parameters> > public> static> void> Area(> out> int> p,> out> int> q,> out> int> Rarea)> > {> > p = 30;> > q = 40;> > Rarea = p * q;> > }> }>

出力:

Length of the rectangle is : 30 Width of the rectangle is : 40 Area of the rectangle is : 1200