「C#」outキーワードでメソッドの戻り値を引数で受け取る

書式
関数名([out 型 変数名] …)
{
//処理コード
}
outキーワードを使用してメソッドの戻り値を引数で受け取ります。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
namespace Study
{
class Program
{
static void Main(string[] args)
{
int x, y;
funA(5, out x, out y);
Console.WriteLine(x);
Console.WriteLine(y);
}
static void funA(int x, out int resA, out int resB)
{
resA = x * 2;
resB = x * 3;
}
}
}
using System; namespace Study { class Program { static void Main(string[] args) { int x, y; funA(5, out x, out y); Console.WriteLine(x); Console.WriteLine(y); } static void funA(int x, out int resA, out int resB) { resA = x * 2; resB = x * 3; } } }
using System;

namespace Study
{
  class Program
  {
    static void Main(string[] args)
    {
       int x, y;
       funA(5, out x, out y);
       
       Console.WriteLine(x);
       Console.WriteLine(y); 
    }
    
    static void funA(int x, out int resA, out int resB)
     {
       resA = x * 2;
       resB = x * 3;
     }
  }
}

実行結果
10
15

C#

Posted by arkgame