C#“或”运算符? [英] C# 'or' operator?

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

问题描述

有一个在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屋!

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