什么是IF语句OR运算符 [英] What is the OR operator in an IF statement
问题描述
在C#中,如何指定OR:
如果(这种或那种){做其他事情}
我找不到它的帮助。
更新:
我的代码是:
如果(标题==用户问候语||用户名){做的东西}
和我的错误是:
错误1操作'||'不能应用于类型为布尔和字符串操作数C:\Documents和Settings\Sky查看Barns\My Documents\Visual工作室2005\Projects\FOL Ministry\FOL Ministry\Downloader.cs 63 21 FOL部
块引用>
解决方案
||
是在C#中有条件或运营商
您可能有一个很难找到它,因为它很难寻找的东西的名字你不知道。下一次尝试做一个谷歌搜索C#操作员,并期待在逻辑运算符。
我的代码是:
如果(标题==用户问候语||用户名){做的东西};
和我的错误是:
错误1操作'||'不能为
应用于类型为布尔和
'串'C的操作数:\Documents和Settings\Sky
查看Barns\My Documents\ Visual Studio的
2005\Projects\FOL Ministry\FOL
Ministry\Downloader.cs 63 21 FOL
部
块引用>
您需要这样做,而不是:
如果(标题==用户问候语||标题==用户名){做的东西};
OR运算符的计算结果相同的方式双方的表达式。在你的榜样,你在表达
标题==用户问候语
(一个布尔值),并表达操作用户名
(字符串)。这些不能被不进行强制转换或转换,这就是为什么你得到错误直接结合起来。
此外,值得一提的是,
||
操作员使用短路计算。这意味着,如果第一个表达式的计算结果为真正
,第二个表达式不评估,因为它不必是 - 最终的结果总是会真正
。有时你可以采取优化过程中利用了这一点。
最后一个快速的注意 - 我经常写我的嵌套的括号条件语句是这样的:
IF((标题==用户问候语)||(标题==用户名)){做的东西};
这样我可以控制的优先级和不必担心操作的顺序。这也可能是矫枉过正这里,但它是当逻辑变得复杂起来特别有用。
In C#, how do I specify OR:
if(this OR that) {do the other thing}
I couldn't find it in the help.
Update:
My code is:
if (title == "User greeting" || "User name") {do stuff}
and my error is:
Error 1 Operator '||' cannot be applied to operands of type 'bool' and 'string' C:\Documents and Settings\Sky View Barns\My Documents\Visual Studio 2005\Projects\FOL Ministry\FOL Ministry\Downloader.cs 63 21 FOL Ministry
解决方案
||
is the conditional OR operator in C#You probably had a hard time finding it because it's difficult to search for something whose name you don't know. Next time try doing a Google search for "C# Operators" and look at the logical operators.
Here is a list of C# operators.
My code is:
if (title == "User greeting" || "User name") {do stuff};
and my error is:
Error 1 Operator '||' cannot be applied to operands of type 'bool' and 'string' C:\Documents and Settings\Sky View Barns\My Documents\Visual Studio 2005\Projects\FOL Ministry\FOL Ministry\Downloader.cs 63 21 FOL Ministry
You need to do this instead:
if (title == "User greeting" || title == "User name") {do stuff};
The OR operator evaluates the expressions on both sides the same way. In your example, you are operating on the expression
title == "User greeting"
(a bool) and the expression"User name"
(a string). These can't be combined directly without a cast or conversion, which is why you're getting the error.In addition, it is worth noting that the
||
operator uses "short-circuit evaluation". This means that if the first expression evaluates totrue
, the second expression is not evaluated because it doesn't have to be - the end result will always betrue
. Sometimes you can take advantage of this during optimization.One last quick note - I often write my conditionals with nested parentheses like this:
if ((title == "User greeting") || (title == "User name")) {do stuff};
This way I can control precedence and don't have to worry about the order of operations. It's probably overkill here, but it's especially useful when the logic gets complicated.
这篇关于什么是IF语句OR运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!