简体   繁体   English

重载方法选择逻辑

[英]Overloaded method selection logic

Given the following overloaded methods: 鉴于以下重载方法:

public string Test(long item)
{
    return "Test with a long was called!";
}

public string Test(int item)
{
    return "Test with an int was called!";
}

public string Test(object item)
{
    return "Test with an object was called!";
}

When I call Test() , passing a short , like this: 当我调用Test() ,传递一个short ,如下所示:

short shortValue = 7;
var result = Test(shortValue);

Why is the value result equal to "Test with an int was called!" 为什么值result等于"Test with an int was called!" , instead of "Test with an object was called!" 而不是"Test with an object was called!" ?

Why is the value result equal to "Test with an int was called!", instead of "Test with an object was called!"? 为什么值结果等于“使用int测试!”而不是“使用对象测试被调用!”?

The conversion to int is "better" than the conversion to object , so the overload taking int is "better" than the one taking object - and both are applicable as short is implicitly convertible to both int and object . 转换为int比转换为object更“好”,因此使用int的重载比获取object “更好” - 两者都适用,因为short可以隐式转换为intobject (The overload taking long is also applicable, but the conversion to int is better than the one to long , too.) (过载走long也很适用,但对转换int比一个更好地long了。)

See section 7.5.3 of the C# language specification for general overloading rules, and 7.5.3.3 for the rules about "better conversions". 有关一般重载规则,请参阅C#语言规范的7.5.3节;有关“更好的转换”的规则,请参阅7.5.3.3。 There's little point in writing them all out here, as they're very long - but the most important aspect is that there's a conversion from int to object but no conversion from object to int - so the conversion to int is more specific, and therefore better. 将它们全部写在这里没有什么意义,因为它们很长 - 但最重要的方面是从int转换为object但没有从object转换为int - 因此转换为int更具体,因此更好。

(Section numbers are from the C# 4 and C# 5 versions. You can download the C# 5 spec in Word format.) (部分编号来自C#4和C#5版本。您可以下载Word格式的C#5规范 。)

The C# specification rules mean that the compiler prefers converting a short to int , not to object . C#规范规则意味着编译器更喜欢将short转换为int ,而不是object I think this is due to the following rule from 7.5.3.5 Better conversion target (link is to C# 5 spec download, or see equivalent from C# 1.2 online) 我认为这是由于以下规则来自7.5.3.5更好的转换目标 (链接是C#5规范下载,或者从C#1.2在线查看等效

Given two different types T1 and T2, T1 is a better conversion target than T2 if at least one of the following holds: 给定两种不同类型T1和T2,如果至少有以下一种情况,则T1是比T2更好的转换目标:

  • An implicit conversion from T1 to T2 exists, and no implicit conversion from T2 to T1 exists 存在从T1到T2的隐式转换,并且不存在从T2到T1的隐式转换
  • T1 is a signed integral type and T2 is an unsigned integral type. T1是有符号整数类型,T2是无符号整数类型。 [other content omitted] [其他内容省略]

To rewrite it for this scenario, since an implicit conversion from int to object exists, and no implicit conversion from object to int exists, converting to int is the better conversion. 要为此场景重写它,因为存在从intobject的隐式转换,并且不存在从objectint隐式转换,转换为int是更好的转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM