我有一个使用params关键字的方法,如下所示:
private void ParamsMethod(params string[] args)
{
// Etc...
}然后,我使用各种参数组合调用该方法:
// Within the method, args is...
ParamsMethod(); // - a string array with no elements
ParamsMethod(null); // - null (Why is this?)
ParamsMethod((string)null); // - a string array with one element: null
ParamsMethod(null, null); // - a string array with two elements: null and null
ParamsMethod("s1"); // - a string array with one element: "s1"
ParamsMethod("s1", "s2"); // - a string array with two elements: "s1" and "s2"我理解所有的案例,除了第二个案例。谁能解释一下为什么ParamsMethod(null)会导致args为null,而不是一个只有一个空元素的数组?
发布于 2012-01-27 05:53:36
从语言规范中:
参数数组允许在方法调用中以两种方式之一指定参数:
由于ParamsMethod(null)中的null可以隐式转换为(string[])null,因此应用的是第一个规则。
https://stackoverflow.com/questions/9025842
复制相似问题