不明确的方法重载 [英] Ambiguous method overloading

查看:149
本文介绍了不明确的方法重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已再度陷入我。可能有人提供了技术解释为什么下面的代码不会产生任何警告或错误。你要问自己的问题是,你觉得幸运的(当然)?



 类节目
{
静态字符串感觉(对象o){返回幸运; }
静态字符串感觉(字符串s){返回倒霉; }

静态无效的主要(字串[] args)
{
Console.WriteLine(我觉得+感觉(NULL));
}
}



奖金奖励,如果你知道哪些方法会被调用点的没有的运行代码。
和刚刚添加的侮辱,它不只是空参数发生:

 类节目
{
静态字符串感觉(int i)以{返回幸运; }
静态字符串感觉(UINT我){返回倒霉; }

静态无效的主要(字串[] args)
{
Console.WriteLine(我觉得+感觉(7));
}
}


解决方案

第一例如:空参数



在第一种情况下它会调用字符串超载。 同时匹配对象字符串,但字符串是更具体的/派生类型。因此,选择字符串



检查埃里克利珀的帖子的怎样的方法重载解析系统决定当空调用哪个方法值传递?的用于重载的这一部分更详细的解释。




现在,我们必须确定最佳的适用候选人。该bestness规则是复杂的,但短期的版本是,更具体的是小于具体越好。




第二个例子:整数文字



在第二种情况下,它会选择第一个过载,因为文字 7 INT 。如果你已经使用 7U 这本来是 UINT ,因此,第二个重载将是首选。



整数具有良好定义的类型(即使他们允许比正常的积分值更隐式转换)。您可以使用像 U 后缀符号,或长期影响该类型。或者你可以添加一个显式类型转换。



虽然通常为 INT 不会隐式转换为 UINT ,这是一个整数的的这是在 UINT 和C#编译器。有一个额外的规则,允许整数间的转换的常量的,只要不断的符合目标的范围。



一再次埃里克解释细节:< A HREF =htt​​p://stackoverflow.com/questions/9008637/why-does-this-implicit-conversion-from-int-to-uint-work/9008765#9008765>为什么从int这个隐式转换为uint工作?




int类型的常量表达式可以转换为类型为sbyte,字节,短,USHORT,uint或ulong,则所提供的常数表达式的值在目标类型的范围内。类型的常量表达式长可以转换为ulong类型,提供的常量表达式的值不为负。







在这两个例子中的一个超载显然是最好的,至于C#编译器而言,这样的话你没有得到一个模棱两可的重载的错误。



我个人认为,第一个例子应该给一个警告,但无论是C#团队有异议的,或者他们根本没有时间补充一点启发。


This issue has caught me out once again. Could someone provide a technical explanation as to why the following code does not produce any warnings or errors. The question you have to ask yourself is (of course) do you feel lucky?

class Program
{
    static string Feeling(object o) { return "Lucky"; }
    static string Feeling(string s) { return "Unlucky"; }

    static void Main(string[] args)
    {
        Console.WriteLine("I feel " + Feeling(null));
    }
}

Bonus points awarded if you know which method will be called without running the code. And just to add insult, it doesn't just happen with null parameters:

class Program
{
    static string Feeling(int i) { return "Lucky"; }
    static string Feeling(uint i) { return "Unlucky"; }

    static void Main(string[] args)
    {
        Console.WriteLine("I feel " + Feeling(7));
    }
}

解决方案

First example: null argument

In the first case it will call the string overload. null matches both object and string, but string is the more specific/derived type. Thus it chooses string.

Check Eric Lippert's post How does the method overload resolution system decide which method to call when a null value is passed? for a longer explanation for this part of overload resolution.

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.

Second example: integer literal

In the second case it'll choose the first overload, because the literal 7 is int. If you had used 7u it would have been uint and thus the second overload would be preferred.

Integer literals have a well defined type(even if they allow more implicit conversions than normal integral values). You can use suffixes like u for unsigned, or l for long to influence that type. Or you can add an explicit cast.

While normally an int wouldn't be implicitly convertible to uint, this is an integer constant which is in the valid range of uint, and the C# compiler has an extra rule to allow implicit conversions between integer constants, provided the constant fits the target's range.

One again Eric explains the details: Why does this implicit conversion from int to uint work?

A constant expression of type int can be converted to type sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant-expression is within the range of the destination type. A constant expression of type long can be converted to type ulong, provided the value of the constant expression is not negative.


In both examples one overload is clearly the best, as far as the C# compiler is concerned, and thus you don't get an ambiguous overloading error.

Personally I think that the first example should give a warning, but either the C# team disagrees, or they simply didn't have time to add that heuristic.

这篇关于不明确的方法重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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