params在传递可变参数或者可变数组是可以用到,例如:(加注释的是只传递可变的数组,没加注释的代码是参数和可变参数同时传递。)
static void Main(string[] args)
{
//string[] aa = {"11","22","33","44" };
//Zhi(aa);
//Zhi("aa", "bb", "cc", "dd", "ee");//可以加任意个参数
SayHello("tom","老特","特2","特哥");
Console.ReadKey();
}
//static void Zhi(params string[] values)
//{
// foreach (string va in values)
// {
// Console.WriteLine(va);
// }
//}
static void SayHello(string name, params string[] nickNames)
{
Console.WriteLine("我的名字是 {0}",name);
foreach (string nickName in nickNames)
{
Console.WriteLine("我的昵称是 {0}",nickName);
}
} |