[英]c# method overload picks the wrong overload and compiles
我有以下内容:
EnErrorCodes WriteToCard(EnRegs address, byte data)
EnErrorCodes WriteToCard(EnRegs address, ushort data)
EnErrorCodes WriteToCard(EnRegs address, uint data)
EnErrorCodes WriteToCard(EnRegs address, float data)
EnErrorCodes WriteToCard(EnRegs address, double data)
但错过了ulong
超载。
然后,当盲目地使用ulong
重载时,编译器选择了float
重载而不是抛出错误。
花了很长时间才弄清楚为什么系统没有正确写入该数据,只有 ctrl+单击调用它的WriteToCard
方法,然后将其带到浮点重载。
创建ulong
显然对它进行了排序,但我不明白它为什么会发生,因为它是一个相当烦人的尝试和查找功能。
这是因为Implicit
数字转换。
如果您查看上图中突出显示的部分,C# 会将ulong
转换为float
、 double
、 decimal
。
由于您有带有float
参数的WriteToCard()
,如果您使用ulong
作为类型传递data
,编译器会将其隐式转换为 float 并执行它,而不是引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.