在C#运算符优先级 [英] Operator precedence in C#

查看:147
本文介绍了在C#运算符优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(int)(int1 / (float)var2.Count() * 100)

等同于

(int)((int1 / (float)var2.Count()) * 100)

.. 。而将其使用浮点或整数除法?

...and will it use floating point or integer division?

编辑...如果答案是肯定的上面,什么是这里执行浮点除法的优势?

Edit... if the answer is yes to the above, what is the advantage of performing a floating point division here?

推荐答案

/ * 有相同的运算符优先级,在7.2.1节这样两个结果应该是相同的(使用浮动规则)。

/ and * have the same operator precedence, under §7.2.1 so the two results should be the same (using float rules).

但是,我也懒得去学习优先表;我只是用括号。然后,它适用于任何语言,而无需记住它。

I, however, can't be bothered to learn precedence tables; I just use brackets. Then it works in any language without needing to remember it.

另一个重要的问题是,在最后的(INT)投:你期望这是向上,向下或银行家

Another important question is the rounding in the final (int) cast: do you expect that to be "up", "down" or "bankers"?

这篇关于在C#运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆