C# 什么是 IF 语句中的 OR 运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2158580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the OR operator in an IF statement
提问by Arlen Beiler
In C#, how do I specify OR:
在 C# 中,如何指定 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
错误 1 运算符“||” 不能应用于“bool”和“string”类型的操作数 C:\Documents and Settings\Sky View Barns\My Documents\Visual Studio 2005\Projects\FOL Ministry\FOL Ministry\Downloader.cs 63 21 FOL Ministry
采纳答案by Andy West
||
is the conditional OR operator in C#
||
是 C# 中的条件 OR 运算符
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.
您可能很难找到它,因为很难搜索您不知道名字的东西。下次尝试在 Google 上搜索“C# 运算符”并查看逻辑运算符。
Here is a listof 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
我的代码是:
if (title == "User greeting" || "User name") {do stuff};
我的错误是:
错误 1 运算符“||” 不能应用于“bool”和“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.
OR 运算符以相同的方式计算两边的表达式。在您的示例中,您正在对表达式title == "User greeting"
(布尔值)和表达式"User name"
(字符串)进行操作。这些不能在没有强制转换或转换的情况下直接组合,这就是您收到错误的原因。
In addition, it is worth noting that the ||
operator uses "short-circuit evaluation". This means that if the first expression evaluates to true
, the second expression is not evaluated because it doesn't have to be - the end result will always be true
. Sometimes you can take advantage of this during optimization.
此外,值得注意的是,||
运算符使用了“短路评估”。这意味着如果第一个表达式的计算结果为true
,则不会计算第二个表达式,因为它不一定是 - 最终结果将始终为true
。有时您可以在优化过程中利用这一点。
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.
这样我就可以控制优先级,而不必担心操作的顺序。这里可能有点矫枉过正,但是当逻辑变得复杂时它特别有用。
回答by blu
Or is ||
或者是 ||
And is &&
并且是 &&
Update for changed question:
更新更改的问题:
You need to specify what you are comparing against in each logical section of the if statement.
您需要在 if 语句的每个逻辑部分中指定要比较的对象。
if (title == "User greeting" || title == "User name")
{
// do stuff
}
回答by womp
The OR operator is a double pipe:
OR 运算符是一个双管道:
||
So it looks like:
所以它看起来像:
if (this || that)
{
//do the other thing
}
EDIT:The reason that your updated attempt isn't working is because the logical operators must separate valid C# expressions. Expressions have operands and operators and operators have an order of precedence.
编辑:您更新的尝试不起作用的原因是逻辑运算符必须分隔有效的 C# 表达式。表达式有操作数和操作符,操作符有优先顺序。
In your case, the ==operator is evaluated first. This means your expression is being evaluated as (title == "User greeting") || "User name"
. The || gets evaluated next. Since || requires each operand to be a boolean expression, it fails, because your operands are strings.
在您的情况下,首先评估==运算符。这意味着您的表达式被评估为(title == "User greeting") || "User name"
. || 接下来进行评估。因为 || 要求每个操作数都是一个布尔表达式,它失败了,因为你的操作数是字符串。
Using two separate boolean expressions will ensure that your ||
operator will work properly.
使用两个单独的布尔表达式将确保您的||
运算符正常工作。
title == "User greeting" || title == "User name"
回答by Lachlan Roche
The conditional or operator is ||:
条件或运算符是 ||:
if (expr1 || expr2) {do stuff}
if (title == "User greeting" || title == "User name") {do stuff}
The conditional (the OR) and it's parts are boolean expressions.
条件(OR)及其部分是布尔表达式。
MSDN lists the C# operators in precedence order here http://msdn.microsoft.com/en-us/library/6a71f45d.aspx. And the MSDN page for boolean expressionsis http://msdn.microsoft.com/en-us/library/dya2szfk.aspx.
MSDN 在此处按优先顺序列出了 C# 运算符http://msdn.microsoft.com/en-us/library/6a71f45d.aspx。布尔表达式的 MSDN 页面是http://msdn.microsoft.com/en-us/library/dya2szfk.aspx。
If you are just starting to learn programming, you should read up on Conditional Statements from an introductory text or tutorial. This one seems to cover most of the basics: http://www.functionx.com/csharp/Lesson10.htm.
如果您刚刚开始学习编程,您应该从介绍性文本或教程中阅读条件语句。这个似乎涵盖了大部分基础知识:http: //www.functionx.com/csharp/Lesson10.htm。
回答by Dilbert61
See C# Operatorsfor C# operators including OR which is ||
请参阅C# 运算符以了解 C# 运算符,包括 OR||
回答by Paul Creasey
you need
你需要
if (title == "User greeting" || title == "User name") {do stuff};
回答by David Hall
Just for completeness, the || and && are the conditional version of the | and & operators.
只是为了完整性, || 和 && 是 | 的条件版本 和 & 运算符。
A reference to the ECMA C# Language specification is here.
此处为 ECMA C# 语言规范的参考。
From the specification:
从规范:
3 The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is false.
3 操作 x || y 对应于操作 x | y,除了仅当 x 为假时才计算 y。
In the |
version both sides are evaluated.
在|
版本中,双方都进行了评估。
The conditional version short circuits evaluation and so allows for code like:
条件版本短路评估,因此允许使用以下代码:
if (x == null || x.Value == 5)
// Do something
Or (no pun intended) using your example:
或者(没有双关语)使用你的例子:
if (title == "User greeting" || title == "User name")
// {do stuff}
回答by Sen
hopefully you would have got an idea by now.if intrested you can also have a look at the c# specification at http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf
希望你现在已经有了一个想法。如果有兴趣,你也可以在http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf查看 c# 规范
回答by Tad Donaghe
The reason this is wrong:
这是错误的原因:
if (title == "User greeting" || "User name") {do stuff};
is because what that's saying is
是因为那句话是
If title equals the string "User greeting"
如果标题等于字符串“用户问候”
or just "User name" (not if title equals the string "User name"). The part after your or would be like writing
或者只是“用户名”(不是如果标题等于字符串“用户名”)。你或之后的部分就像写作
if ("User name")
which c# doesn't know what to do with. It can't figure out how to get a boolean out of "User name"
哪个 c# 不知道该怎么做。它无法弄清楚如何从“用户名”中获取布尔值
回答by fastcodejava
In the format for if
格式为 if
if (this OR that)
this
and that
are expression not values. title == "aaaaa"
is a valid expression. Also OR
is not a valid construct in C#, you have to use ||
.
this
并且that
是不表达值。title == "aaaaa"
是一个有效的表达式。另外OR
是不是在C#有效的结构,你必须使用||
。