namespace parmars
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int sumvalue(params int[] vals) //参数是用params修饰的数组 在调用函数是形参才会被初始化 所以实参的个数是任意的,但必须是int型参数,而且params参数数组只能放在参数表的最后面
{
int sum = 0;
foreach (int val in vals)
{
sum += val;
}
return sum;
}
private void button1_Click(object sender, EventArgs e)
{
int sum = sumvalue(1, 2, 3, 4, 5);
int sum2 = sumvalue(298, 3434, 232);
label1.Text = sum.ToString();
label2.Text = sum2.ToString();
}
}
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int sumvalue(params int[] vals) //参数是用params修饰的数组 在调用函数是形参才会被初始化 所以实参的个数是任意的,但必须是int型参数,而且params参数数组只能放在参数表的最后面
{
int sum = 0;
foreach (int val in vals)
{
sum += val;
}
return sum;
}
private void button1_Click(object sender, EventArgs e)
{
int sum = sumvalue(1, 2, 3, 4, 5);
int sum2 = sumvalue(298, 3434, 232);
label1.Text = sum.ToString();
label2.Text = sum2.ToString();
}
}
}