下面的代码解释了问题(使用运行时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"); } }
解决方法
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 举报,一经查实,本站将立刻删除。