- Console.WriteLine(Sum());
- Console.WriteLine(Sum(1));
- Console.WriteLine(Sum(1,2));
- Console.WriteLine(Sum(1, 2,3));
既有固定参数 和不定参数方法的调用
Test(10, "hello world", true);
a,b 是不定参数数组的元素
10, "hello world" 是属于固定参数
Test(10, "hello world", true,"a","b");
参数列表格式,如果有一定数量的参数用,隔开
参数个数不确定 可以使用 params 数据类型[] 参数名字进行定义;
params int[] nums;
不定参数关键字 params
- public static int Sum(params int[] nums)
- {
- Console.WriteLine("传入参数个数为:"+nums.Length);
- int sum = 0;
- for (int i = 0; i < nums.Length; i++)
- {
- Console.WriteLine("参数是:" + nums[i]);
- sum += nums[i];
- }
-
- return sum;
- }
固定参数和不定参数个数的方法声明,先把固定的参数写在前面,不定参数个位数写在后面
- public static void Test(int a,string b,bool c,params string[] n1)
- {
- Console.WriteLine(a + "," + b + "," + c);
- }