IF 语句中的 OR 运算符是什么
在C#中,如何指定OR:
if(this OR that) {do the other thing}
我在帮助中找不到它。
更新:
我的代码是:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
||
是 C# 中的条件 OR 运算符,您可能很难找到它,因为很难搜索您不知道名称的内容。下次尝试在 Google 上搜索“C# 运算符”并查看逻辑运算符。
这里是 C# 运算符的列表。
您需要这样做:
OR 运算符以相同的方式计算两边的表达式。在您的示例中,您正在对表达式
title == "Usergreeting"
(布尔值)和表达式"User name"
(字符串)进行操作。如果不进行强制转换或转换,则无法直接组合它们,这就是您收到错误的原因。另外,值得注意的是
||
运算符使用“短路求值”。这意味着,如果第一个表达式的计算结果为true
,则不会计算第二个表达式,因为它不必如此 - 最终结果将始终为true
。有时您可以在优化过程中利用这一点。最后一个快速说明 - 我经常用嵌套括号编写条件,如下所示:
这样我可以控制优先级,而不必担心操作顺序。在这里它可能有点过分了,但是当逻辑变得复杂时它特别有用。
||
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.
You need to do this instead:
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:
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.
OR 运算符是双管道:
所以它看起来像:
编辑: 您更新的尝试不起作用的原因是逻辑运算符必须分隔有效的 C# 表达式。表达式具有操作数和运算符,并且运算符具有优先顺序。
在您的情况下,首先评估 == 运算符。这意味着您的表达式被评估为
(title == "Usergreeting") || “用户名”
。 ||接下来进行评估。自从 ||要求每个操作数都是布尔表达式,它会失败,因为你的操作数是字符串。使用两个单独的布尔表达式将确保您的
||
运算符正常工作。The OR operator is a double pipe:
So it looks like:
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.
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.Using two separate boolean expressions will ensure that your
||
operator will work properly.为了完整起见,||和&&是 | 的条件版本和&运营商。
此处引用了 ECMA C# 语言规范。
从规格来看:
在
|
版本中,双方都会被评估。条件版本短路评估,因此允许使用以下代码:
或者(没有双关语)使用您的示例:
Just for completeness, the || and && are the conditional version of the | and & operators.
A reference to the ECMA C# Language specification is here.
From the specification:
In the
|
version both sides are evaluated.The conditional version short circuits evaluation and so allows for code like:
Or (no pun intended) using your example:
你需要
you need
条件或运算符是 ||:
条件(OR)及其部分是布尔表达式。
MSDN 在此处按优先顺序列出了 C# 运算符 http://msdn.microsoft.com /en-us/library/6a71f45d.aspx 。 布尔表达式的 MSDN 页面是 http:// /msdn.microsoft.com/en-us/library/dya2szfk.aspx。
如果您刚刚开始学习编程,您应该阅读介绍性文本或教程中的条件语句。这似乎涵盖了大部分基础知识: http://www.functionx.com/csharp/ Lesson10.htm 。
The conditional or operator is ||:
The conditional (the OR) and it's parts are boolean expressions.
MSDN lists the C# operators in precedence order here http://msdn.microsoft.com/en-us/library/6a71f45d.aspx . And the MSDN page for boolean expressions is 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 .
或者是||
并且是&&
更改问题的更新:
您需要在 if 语句的每个逻辑部分中指定要比较的内容。
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.
在 C#9 中,我们现在可以使用模式组合符“or”、“and”和“not”作为“is”条件检查中的运算符。
请参阅此内容了解详细信息。
所以你的代码
……可以直接写成:
In C#9 we can now use the pattern combinators "or", "and" and "not" as an operator in an "is" condition check.
See this for more information.
So your code ...
... could be directly written as:
请参阅 C# 运算符,了解 C# 运算符,包括 OR(
|) |
See C# Operators for C# operators including OR which is
||
这是错误的原因:
因为它的意思是
如果标题等于字符串“用户问候语”
或只是“用户名”(而不是如果标题等于字符串“用户名”)。 or 之后的部分就像写
c# 不知道该怎么办。它无法弄清楚如何从“用户名”中获取布尔值
The reason this is wrong:
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
which c# doesn't know what to do with. It can't figure out how to get a boolean out of "User name"
格式为 if
this
和that
是表达式而不是值。title == "aaaaa"
是一个有效的表达式。另外,OR
在 C# 中不是有效的构造,您必须使用||
。In the format for if
this
andthat
are expression not values.title == "aaaaa"
is a valid expression. AlsoOR
is not a valid construct in C#, you have to use||
.您应该使用
equal
像You should use
equal
likeOR 用作“||”
OR is used as "||"
您可以同时使用 &&或||
You can use both && or ||
那也是我的代码。我就这样解决了。
if ((title == "用户问候语") || (title == "用户名")) {do stuff}
That was my code too. I solved it like that.
if ((title == "User greeting") || (title == "User name")) {do stuff}