LoginSignup
0
0

More than 3 years have passed since last update.

C#で可変長の引数を持つ関数の実装

Last updated at Posted at 2019-09-20

自作の計算関数を作成するときに気になったので自分用メモ

void Foo(params int[] a)
{
   // 処理
}

static void Main(string[] args)
{
    int a = 1, b = 2, ...;
    // 無限(有限)に引数が増やせる
    Foo(a, b, ...);
}

配列を渡したい場合

void Foo(params int[][] a)
{
    // 各配列の要素が欲しいときは下記のように
    // a[第1引数][0番目]
    var tmp = a[0][0];
}

static void Main(string[] args)
{
    int []a = {1, 2, ...};
    int []b = {1, 3, ...};
    ...

    Foo(a, b, ...);
}

一応の注意ですが
...は適宜増やしてくださいの意なので
そのままコピペして動かさないでくださいね

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0