如何C#有歧义,而params选择 [英] How does C# choose with ambiguity and params

查看:189
本文介绍了如何C#有歧义,而params选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下几种方法:

public static void MyCoolMethod(params object[] allObjects)
{
}

public static void MyCoolMethod(object oneAlone, params object[] restOfTheObjects)
{
}

如果我这样做:

MyCoolMethod("Hi", "test");

其中一个被调用,为什么?

which one gets called and why?

推荐答案

这很容易测试 - 第二个方法被调用

It's easy to test - the second method gets called.

至于为什么 - 对C#语言规范有一定的pretty不明的函数声明得到解决方式的详细规则。有很多对SO周边接口,继承问题和过载与为什么不同的重载被调用一些具体的例子,但是要回答这个特定的实例:

As to why - the C# language specification has some pretty detailed rules about how ambiguous function declarations get resolved. There are lots of questions on SO surrounding interfaces, inheritance and overloads with some specific examples of why different overloads get called, but to answer this specific instance:

<一个href=\"http://www.microsoft.com/downloads/details.aspx?familyid=DFBF523C-F98C-4804-AFBD-459E846B268E&displaylang=en\">C#规范 - 重载解析

7.5.3.2更好的函数成员

7.5.3.2 Better function member

有关决定的目的
  更好的功能部件,
  简装参数列表A是
  构建只包含
  参数前pressions自己在
  为了它们在原始
  参数列表。

For the purposes of determining the better function member, a stripped-down argument list A is constructed containing just the argument expressions themselves in the order they appear in the original argument list.

为每个参数列表
  候选函数成员
  以下列方式构建的:

Parameter lists for each of the candidate function members are constructed in the following way:


      
  • 以展开形式使用,如果
      函数成员是适用
      仅在展开形式

  • The expanded form is used if the function member was applicable only in the expanded form.

可选不带参数
  相应的参数被删除
  从参数列表

Optional parameters with no corresponding arguments are removed from the parameter list

的参数重新排序
  以便它们发生在同一
  位置的相应参数
  在参数列表中。

The parameters are reordered so that they occur at the same position as the corresponding argument in the argument list.

和进一步的...

在的情况下,参数型序列{P1,P2,...,PN}和{Q1,Q2,...,QN}是等价>(即,每个皮有一个标识转换成相应的齐),以下
  平分决胜规则应用,才能,确定更好的函数成员。

In case the parameter type sequences {P1, P2, …, PN} and {Q1, Q2, …, QN} are equivalent > (i.e. each Pi has an identity conversion to the corresponding Qi), the following tie-breaking rules are applied, in order, to determine the better function member.


      
  • 如果MP是非泛型方法而MQ是泛型方法,则MP比MQ好。

  • If MP is a non-generic method and MQ is a generic method, then MP is better than MQ.

否则,如果MP适用于正常形式和MQ有一个参数数组,并
  仅适用于以展开形式,则MP比MQ好。

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.

否则,如果MP具有比MQ更宣称的参数,则MP比MQ好。
  如果这两种方法有PARAMS阵列和只在适用这可能发生其
  扩展形式。

粗体tie-breaking规则似乎正是在这种情况下应用。该规范将详细了解PARAMS阵列如何在正常处理和扩展形式,但最终经验法则是,最具体的过载将在参数数量和类型方面被调用。

The bolded tie-breaking rule seems to be what is applying in this case. The specification goes into detail about how the params arrays are treated in normal and expanded forms, but ultimately the rule of thumb is that the most specific overload will be called in terms of number and type of parameters.

这篇关于如何C#有歧义,而params选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆