1. 上测试代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Leaf
{
public class Test
{
public static void Main(string[] args)
{
Console.WriteLine("A. 传3个参数: ");
TestMethod(new string[]{"1","2","3"});
Console.WriteLine("\nB. 传2个参数: ");
TestMethod(1,"2");
Console.WriteLine("\nC. 不传参数");
TestMethod();
Console.ReadLine();
}
public static void TestMethod(params object[] objArr)
{
foreach(object obj in objArr)
Console.WriteLine(obj);
}
}
}
2. 结果
由此可见 params 有如下特性:
1. 可以传入数组、多个单独值(必须同一类型)、甚至可以不传值;
2. 不传值的情况下, params形参仍有效, 不会为null, 但长度为0.