a | = b,a | b和a || b(为什么不是|| = b?) [英] a |=b, a | b and a || b (why not a ||= b?)
问题描述
为什么不能使用|| = b?为了提高效率,我只想对
进行必要的评估。我必须把它写成更长的吗?方式(a =
a || b)?
谢谢,
Mats-Lennart
Mats-Lennart Hansson< ap ******** @ hotmail.com>写道:为什么不能使用|| = b?为了提高效率,我只想尽可能多地评估。我必须把它写成更长的吗?方式(a =
a || b)?
我怀疑它是因为那将是一个很少使用的运算符 - 它是'
我认为通常不会以这种方式使用布尔值。写作
在我看来,漫长的道路也更清晰。
-
Jon Skeet - < sk * **@pobox.com>
http://www.pobox.com/ 〜双向飞碟
如果回复小组,请不要给我发邮件
| =运算符就是你的''正在寻找。它适用于布尔值
值。
" Mats-Lennart Hansson"写道:
为什么不能使用|| = b?为了提高效率,我只想尽可能多地评估。我必须把它写成更长的吗?方式(a =
a || b)?
谢谢,
Mats-Lennart
Jeremy Davis< Je ********* @ discussion.microsoft.com>写道:| =运算符是你正在寻找的。它在boolean
值上运行得很好。
是的,但它没有短路,这就是OP想要的,我是/>
相信。实际上:
if(!a)
{
a = b;
}
(其中b可能是方法调用或属性)。
-
Jon Skeet - < sk ***@pobox.com>
http://www.pobox.com /〜双向飞碟
如果回复小组,请不要给我发邮件
Hi,
Why isn''t it possible to use a ||= b? To increase efficiency, I only want to
evaluate as much as neccessary. Do I have to write it the "longer" way (a =
a || b)?
Thanks,
Mats-Lennart
Mats-Lennart Hansson <ap********@hotmail.com> wrote:Why isn''t it possible to use a ||= b? To increase efficiency, I only want to
evaluate as much as neccessary. Do I have to write it the "longer" way (a =
a || b)?
I suspect it''s because that would be a very rarely used operator - it''s
not that often that booleans are used in that way, I believe. Writing
it the long way is also clearer, in my view.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
The |= operator is what you''re looking for. It works just fine on boolean
values.
"Mats-Lennart Hansson" wrote:
Hi,
Why isn''t it possible to use a ||= b? To increase efficiency, I only want to
evaluate as much as neccessary. Do I have to write it the "longer" way (a =
a || b)?
Thanks,
Mats-Lennart
Jeremy Davis <Je*********@discussions.microsoft.com> wrote:The |= operator is what you''re looking for. It works just fine on boolean
values.
Yes, but it doesn''t short-circuit, which is what the OP wanted, I
believe. Effectively:
if (!a)
{
a = b;
}
(where b may be a method call or property).
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于a | = b,a | b和a || b(为什么不是|| = b?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!