Params:params 关键字可以指定在参数数目可变处采用参数的方法参数。

Params:params 关键字可以指定在参数数目可变处采用参数的方法参数。

注意点:

1、一个方法中只能使用一个params来声明不定长参数数组;

2、params参数数组只能放在已定义参数后面

3、在方法声明中的 params 关键字之后不允许任何其他参数

示例代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 namespace ParamsUse
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Console.WriteLine(Sum(2, 4, 6, 8, 10));
13             ListParams("江西省", "宜春市", "高安市");
14             Console.ReadKey();
15         }
16         public static int Sum(params int[] intparams)
17         {
18             int sum = 0;
19             for (int i = 0; i < intparams.Length; i++)
20             {
21                 sum += intparams[i];
22             }
23             return sum;
24         }
25         static void ListParams(params string[] strs)
26         {
27             foreach (var item in strs)
28             {
29                 Console.WriteLine(item);
30             }
31         }
32     }
33 }

猜你喜欢

转载自www.cnblogs.com/hardenzhao/p/12264322.html