我有一个使用 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
,而不是一个含有一个空元素的数组吗?
void M(object){}
和void M(params object[]){}
。你调用M(null)
。哪个重载被选择,传递了什么参数以及为什么? - Eric Lippert