解决params传递的参数的可变数量时有什么规则?
假设我有代码:
public void Method(params object[] objects) { } public void Method(IMyInterface intf,params object[] objects) { }
解决方法
这是由C#语言规范回答的:
7.5.3.1 Applicable function member
[…]
Otherwise,if MP is applicable in its normal form and MQ has a
params array and is applicable only in its expanded form,then MP is
better than MQ.Otherwise,if MP has more declared parameters than MQ,then MP is
better than MQ. This can occur if both methods haveparams
arrays
and are applicable only in their expanded forms.[…]
在您的示例中,两个重载仅适用于扩展形式.由于第二个具有更多的声明参数会更好.
在规范的上下文中,一个超过所有其他的重载意味着编译器选择它来绑定该调用,正如在讨论的示例中发生的那样(如果没有一个超负载比所有其他的更好,结果是由于模糊而导致的编译时错误).