
static voID Main(string[] args) { DoSomething(0); Console.Read(); } public static voID DoSomething(int? value) { Console.Writeline("Do Something int? called"); } public static voID DoSomething(MyEnum value) { Console.Writeline("Do Something MyEnum called"); } public static enum MyEnum : int { test }} 您在DoSomething行中收到错误:
Error 1 The call is ambiguous between the following methods or propertIEs: ‘DoSomething(int?)’ and ‘DoSomething(MyEnum)’
但是,如果将零更改为任何其他数字,则不会有这样的问题.
解决方法 C#语言规范说明了从整数文字0到任何枚举类型的隐式转换:13.1.3 Implicit enumeration conversionsAn implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type.
因此,任何其他整数文字只能转换为int?因此是明确的;但是文字0可以转换成两个int?和你的枚举
您的枚举中定义了哪些值无关紧要.即使你有一个枚举,如枚举Test {Cats = 7},以上仍然适用.请记住,所有枚举都可以具有其基础整数类型的所有值,并不限于实际声明的值.
总结以上是内存溢出为你收集整理的任何人都可以解释这个有趣的C#问题吗?全部内容,希望文章能够帮你解决任何人都可以解释这个有趣的C#问题吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)