C#“或”运算符? [英] C# 'or' operator?
问题描述
有一个或
在C#中运营商
我想要做的:
if (ActionsLogWriter.Close or ErrorDumpWriter.Close == true)
{
// Do stuff here
}
但我不知道我怎么会做这样的事情。
But I'm not sure how I could do something like that.
推荐答案
C#支持两个布尔或
运营商:单杆 |
和双栏 ||
。
C# supports two boolean or
operators: the single bar |
and the double-bar ||
.
不同的是, |
总是检查左边和右边的条件,而 ||
只检查右侧的状态,如果有必要(如果左边的计算结果为假)。
The difference is that |
always checks both the left and right conditions, while ||
only checks the right-side condition if it's necessary (if the left side evaluates to false).
这是显著当在右侧的条件涉及在副作用的处理或结果。 (例如,如果你的 ErrorDumpWriter.Close
方法花费一段时间才能完成或改变的东西的状态。)
This is significant when the condition on the right-side involves processing or results in side effects. (For example, if your ErrorDumpWriter.Close
method took a while to complete or changed something's state.)
这篇关于C#“或”运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!