请问该方法重载解析系统决定什么时候一个空值传递给调用哪个方法? [英] How does the method overload resolution system decide which method to call when a null value is passed?

查看:181
本文介绍了请问该方法重载解析系统决定什么时候一个空值传递给调用哪个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,例如,你有这样一个类型:

So for instance you have a type like:

public class EffectOptions
{
    public EffectOptions ( params object [ ] options ) {}

    public EffectOptions ( IEnumerable<object> options ) {}

    public EffectOptions ( string name ) {}

    public EffectOptions ( object owner ) {}

    public EffectOptions ( int count ) {}

    public EffectOptions ( Point point ) {}

}

在这里,我只是给这个例子使用构造,但结果将是相同的,如果他们是在该类型本身的非构造方法吧?

Here I just give the example using constructors but the result will be the same if they were non-constructor methods on the type itself, right?

所以,当你做的:

EffectOptions options = new EffectOptions (null);

该构造函数被调用,为什么?

which constructor would be called, and why?

我可以测试这个自己,但我想了解重载解析系统是如何工作的(不知道这是它叫什么)。

I could test this myself but I want to understand how the overload resolution system works (not sure if that's what it's called).

推荐答案

有关确切的规则,看到的重载决议规范。但是,简单地说,它是这样的。

For the exact rules, see the overload resolution spec. But briefly, it goes like this.

首先,将列表中的所有的访问的构造。

First, make a list of all the accessible constructors.

public EffectOptions ( params object [ ] options )
public EffectOptions ( IEnumerable<object> options ) 
public EffectOptions ( string name )
public EffectOptions ( object owner ) 
public EffectOptions ( int count ) 
public EffectOptions ( Point point )

其次,消除所有的不适用的构造。一个适用的构造方法是,每一个形式参数都有相应的参数,参数是隐式转换为正式参数类型。假设点是值类型,我们消除了INT和点的版本。这使得

Next, eliminate all the inapplicable constructors. An applicable constructor is one where every formal parameter has a corresponding argument, and the argument is implicitly convertible to the formal parameter type. Assuming that Point is a value type, we eliminate the "int" and "Point" versions. That leaves

public EffectOptions ( params object[] options )
public EffectOptions ( IEnumerable<object> options ) 
public EffectOptions ( string name )
public EffectOptions ( object owner ) 

现在,我们要考虑一个与PARAMS是否适用于它的扩展未展开的形式。在这种情况下,它是适用于这两种形式。发生这种情况时,我们丢弃的扩展的形式。使叶片

Now, we have to consider whether the one with "params" is applicable in its expanded or unexpanded form. In this case it is applicable in both forms. When that happens, we discard the expanded form. So that leaves

public EffectOptions ( object[] options )
public EffectOptions ( IEnumerable<object> options ) 
public EffectOptions ( string name )
public EffectOptions ( object owner ) 

现在,我们必须确定适用的候选人的最佳的。该bestness规则是复杂的,但短期的版本是更具体的是小于特定更好。长颈鹿是哺乳动物相比更具体,哺乳动物比动物更具体的,动物比目标更明确。

Now we must determine the best of the applicable candidates. The bestness rules are complicated, but the short version is that more specific is better than less specific. Giraffe is more specific than Mammal, Mammal is more specific than Animal, Animal is more specific than object.

的对象的版本比所有这些不太具体,因此它可以被消除。该的IEnumerable&LT;对象&gt; 版本比 [对象] 版本不太具体(?你知道为什么),所以它可也被淘汰。这使得

The "object" version is less specific than all of them, so it can be eliminated. The IEnumerable<object> version is less specific than the object[] version (do you see why?) so it can be eliminated too. That leaves

public EffectOptions ( object[] options )
public EffectOptions ( string name )

现在,我们被卡住了。 [对象]是既不多也不少具体的不是字符串。因此,这给出了一个含糊不清的错误。

And now we are stuck. object[] is neither more nor less specific than string. Therefore this gives an ambiguity error.

这仅仅是一个简单的草图;真正难解难分算法复杂得多。但是,这些都是基础知识。

That is just a brief sketch; the real tiebreaking algorithm is much more complicated. But those are the basics.

这篇关于请问该方法重载解析系统决定什么时候一个空值传递给调用哪个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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