为什么除法(/)运算符表现不同的VB.NET和C#? [英] Why do the division (/) operators behave differently in VB.NET and C#?
问题描述
如果您在创建C#和VB.NET的新项目,然后直接转到立即窗口并输入:
If you create new projects in C# and VB.NET, then go directly in the Immediate Window and type this:
? 567 / 1000
C#将返回0,而VB.NET将返回0.567。
C# will return 0, while VB.NET will return 0.567.
要获得在C#中相同的结果,你需要键入
To get the same result in C#, you need to type
? 567 / 1000.0
为什么会有这种差异?为什么C#需要明确的小数点经过1000?
Why is there this difference? Why does C# require the explicit decimal point after 1000?
推荐答案
借助 /操作员在C#一>为整数操作数,那么整数除法操作(等同于的 \\运营商在VB.NET )。对于VB.NET,这是正常的划分(将给予分数结果)。在C#中,为了做到这一点,你必须投下至少一个操作数为浮点类型(例如,双击
)明确。
The / operator in C# for integer operands does the "integer division" operation (equivalent to \ operator in VB.NET). For VB.NET, it's the "normal" division (will give fractional result). In C#, in order to do that, you'll have to cast at least one operand to a floating point type (e.g. double
) explicitly.
这篇关于为什么除法(/)运算符表现不同的VB.NET和C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!