2012-07-26 109 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