2012-07-26 71 views
-1

三元运算符有一个简写,即速记三元运算符是否进行多重计算?

var = exp ?: exp2

我知道它在PHP。其他语言也可能挑选它。 C#具有类似的功能(针对此问题的上下文) - ??

当条件通过时,是表达式再次求值,或者在结果存储在某处?

+0

等等,那么问题是什么语言? – domsson 2017-07-24 13:20:05

回答

0

http://msdn.microsoft.com/en-us/library/ms173224.aspx

The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.

它存储,不计算两次。

+0

没错,我想不应该比较。关于最初的运营商呢? – JNF 2012-07-26 08:07:11

+0

然而,在PHP中,表达式每次都被评估,例如:php -r“echo rand(0,1)?:-999;” 这将打印-999多次。这是因为expr1?:expr2返回expr1而不是表达式的结果。 – Marii 2012-07-26 09:02:58

+0

你似乎是自相矛盾,你写它返回expr1,但打印-999? – JNF 2012-07-30 06:25:29