微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

奇怪(可能是错误的)C#编译器行为与方法重载和枚举

今天我发现一个非常奇怪的行为,C#函数重载.当我有一个有2个重载的方法,一个接受Object,另一个接受任何类型的枚举时,就会出现这个问题.当我通过0作为参数时,调用方法的枚举版本.当我使用任何其他整数值时,将调用Object版本.我知道这可以通过使用显式转换来解决,但是我想知道编译器为什么这样做.这是一个错误还是只是一些陌生的语言规则我不知道?

下面的代码解释了问题(使用运行时2.0.50727检查)

感谢您的任何帮助,
Grzegorz Kyc

class Program
{
    enum Bar
    {
        Value1,Value2,Value3
    }

    static void Main(string[] args)
    {
        Foo(0);
        Foo(1);
        Console.ReadLine();
    }

    static void Foo(object a)
    {
        Console.WriteLine("object");
    }

    static void Foo(Bar a)
    {
        Console.WriteLine("enum");
    }
}

解决方法

这可能是你不知道有一个从constant1为0到任何枚举的隐式转换:
Bar x = 0; // Implicit conversion

现在,从0到Bar的转换比从0到对象的转换更具体,这就是为什么使用Foo(Bar)重载.

这是否清楚了一切?

1 Microsoft C#编译器实际上有一个错误,它允许它为任何零常量,而不仅仅是一个整数:

const decimal DecimalZero = 0.0m;

...
Bar x = DecimalZero;

这不太可能会被修复,因为它可能会破坏现有的工作代码.我相信Eric Lippert有两个blog posts,更详细.

C#规范部分6.1.3(C#4规范)有此说法:

An implicit enumeration conversion
permits the decimal-integer-literal 0
to be converted to any enum-type and
to any nullable-type whose underlying
type is an enum-type. In the latter
case the conversion is evaluated by
converting to the underlying enum-type
and wrapping the result (§4.1.10).

这实际上表明该错误不仅仅是允许错误的类型,而是允许任何常量0值被转换,而不仅仅是字面值0.

编辑:它看起来像“恒定”部分是partially introduced in the C# 3 compiler.以前它是一些不变的值,现在看起来像是所有的.

原文地址:https://www.jb51.cc/csharp/95845.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐