当你写了一个方法,这个方法需要对传进来的参数进行加工,但是不确定传递的参数的数量的时候
比如,public void int jiafa(int a,int b){a+b;}
jiafa(1,2)
但是突然需求改了,要传递4个参数,那么就需要改方法。jiafa(1,2,3,4)
此时可以使用params
public void int jiafa(params in[] a){ a.Sum();}
params关键字的作用
(1)、使用 params关键字可以指定一个方法参数,该方法参数的数目可变。
(2)、可以发送参数声明中所指定类型的逗号分隔的参数列表或指定类型的参数数组。 还可以不发送参数。 如果未发送任何参数,则 params
列表的长度为零。
(3)、在方法声明中的 params
关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params
关键字。