C#中params使用

时间:2023-03-09 22:11:31
C#中params使用

1、参数被params修饰即为可变参数,params只能修饰一维数组。

2、给可变参数赋值的时候,可以直接传递数组的元素。

3、在调用的时候,会自动将这些元素封装为一个数组,并将数组传递。

4、可变参数必须放在方法参数的最后。

Eg:

static void TestParams(params int[] arr)

{

   //方法内容

}

static void TestParams(int i,int j,params int[] arr)//需放在最后

{

   //方法内容

}

static void Main(string[] args)

{

    int arr={,,,,,,};

    TestParams(arr);

    //亦可如下

    //TestParams(1,2,3,4,5);

}

注:以上内容均属软谋原创,转载请注明出处。