It may be that you're not aware that there's an implicit conversion from a constant of 0 to any enum:Bar x = 0; // Implicit conversion现在,从 0 到 Bar 的转换比从 0 到 object 的转换更具体,这就是为什么 Foo(Bar)> 使用了重载.Now, the conversion from 0 to Bar is more specific than the conversion from 0 to object, which is why the Foo(Bar) overload is used.这能解决所有问题吗? Microsoft C# 编译器中实际上存在一个错误,它允许它是任何 零常量,而不仅仅是一个整数: There's actually a bug in the Microsoft C# compiler which lets it be any zero constant, not just an integer:const decimal DecimalZero = 0.0m;...Bar x = DecimalZero;这不太可能被修复,因为它可能会破坏现有的工作代码.我相信 Eric Lippert 有两个 博客 帖子 更详细.It's unlikely that this will ever be fixed, as it could break existing working code. I believe Eric Lippert has a two blog posts which go into much more detail.C# 规范第 6.1.3 节(C# 4 规范)对此进行了说明:The C# specification section 6.1.3 (C# 4 spec) has this to say about it:隐式枚举转换允许 decimal-integer-literal 0转换为任何枚举类型和任何可空类型type 是一个枚举类型.在后者如果转换由转换为底层enum-type并包装结果(第 4.1.10 节).An implicit enumeration conversionpermits the decimal-integer-literal 0to be converted to any enum-type andto any nullable-type whose underlyingtype is an enum-type. In the lattercase the conversion is evaluated byconverting to the underlying enum-typeand wrapping the result (§4.1.10).这实际上表明该错误不仅在于允许错误的类型,还在于允许转换任何常量 0 值,而不仅仅是字面值 0.That actually suggests that the bug isn't just in allowing the wrong type, but allowing any constant 0 value to be converted rather than only the literal value 0.它看起来像常数"部分是 在 C# 3 编译器中部分引入.以前它是一些常量值,现在看起来它是全部. It looks like the "constant" part was partially introduced in the C# 3 compiler. Previously it was some constant values, now it looks like it's all of them. 这篇关于奇怪的(可能是错误的?)带有方法重载和枚举的 C# 编译器行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-04 14:22