params将实参列表中跟可变参数数组类型一致的元素都当作数组的元素去处理。
传统写法:
using System;
using System.Linq;
namespace ref和out
{
class Program
{
static void Main(string[] args)
{
int[] score = { 88, 22, 66,55 };
ave(score);
Console.Read();
}
static void ave(int[] score)
{
Console.WriteLine(score.Average());
}
}
}
利用params,省略掉数组的声明:
using System;
using System.Linq;
namespace ref和out
{
class Program
{
static void Main(string[] args)
{
//int[] score = { 88, 22, 66,55 };
ave(88,22,33);
Console.Read();
}
static void ave(params int[] score)
{
Console.WriteLine(score.Average());
}
}
}
只要是同类型,参数的个数不用固定写死,params修饰的参数必须是形参列表中的最后一个,并且只能有一个