How does the method overload resolution system decide which method to call when a null value is passed?(方法重载解析系统如何决定在传递空值时调用哪个方法?)
问题描述
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?
So when you do:
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 )
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 )
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 )
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.
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.
这篇关于方法重载解析系统如何决定在传递空值时调用哪个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:方法重载解析系统如何决定在传递空值时调用哪个方法?


- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01