C# —— 方法的参数列表

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值