IF 语句中的 OR 运算符是什么

发布于 2024-08-20 01:03:03 字数 463 浏览 6 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

早茶月光 2024-08-27 01:03:03

|| 是 C# 中的条件 OR 运算符,

您可能很难找到它,因为很难搜索您不知道名称的内容。下次尝试在 Google 上搜索“C# 运算符”并查看逻辑运算符。

这里是 C# 运算符的列表

我的代码是:

if (title == "用户问候语" || "用户名") {do stuff};

我的错误是:

错误 1 ​​运算符“||”不能是
应用于“bool”类型的操作数和
'字符串' C:\Documents and Settings\Sky
查看 Barns\My Documents\Visual Studio
2005\项目\FOL事工\FOL
部\Downloader.cs 63 21 FOL
部委

您需要这样做:

if (title == "User greeting" || title == "User name") {do stuff};

OR 运算符以相同的方式计算两边的表达式。在您的示例中,您正在对表达式 title == "Usergreeting" (布尔值)和表达式 "User name" (字符串)进行操作。如果不进行强制转换或转换,则无法直接组合它们,这就是您收到错误的原因。

另外,值得注意的是||运算符使用“短路求值”。这意味着,如果第一个表达式的计算结果为 true,则不会计算第二个表达式,因为它不必如此 - 最终结果将始终为 true。有时您可以在优化过程中利用这一点。

最后一个快速说明 - 我经常用嵌套括号编写条件,如下所示:

if ((title == "User greeting") || (title == "User name")) {do stuff};

这样我可以控制优先级,而不必担心操作顺序。在这里它可能有点过分了,但是当逻辑变得复杂时它特别有用。

|| 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 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.

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.

披肩女神 2024-08-27 01:03:03

OR 运算符是双管道:

||

所以它看起来像:

if (this || that) 
{
  //do the other thing
}

编辑: 您更新的尝试不起作用的原因是逻辑运算符必须分隔有效的 C# 表达式。表达式具有操作数和运算符,并且运算符具有优先顺序。

在您的情况下,首先评估 == 运算符。这意味着您的表达式被评估为 (title == "Usergreeting") || “用户名”。 ||接下来进行评估。自从 ||要求每个操作数都是布尔表达式,它会失败,因为你的操作数是字符串。

使用两个单独的布尔表达式将确保您的 || 运算符正常工作。

title == "User greeting" || title == "User name"

The OR operator is a double pipe:

||

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.

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.

title == "User greeting" || title == "User name"
本王不退位尔等都是臣 2024-08-27 01:03:03

为了完整起见,||和&&是 | 的条件版本和&运营商。

此处引用了 ECMA C# 语言规范。

从规格来看:

3 运算 x || y 对应于
操作 x | y,除了 y 是
仅当 x 为 false 时才进行评估。

| 版本中,双方都会被评估。

条件版本短路评估,因此允许使用以下代码:

if (x == null || x.Value == 5)
    // Do something 

或者(没有双关语)使用您的示例:

if (title == "User greeting" || title == "User name") 
    // {do stuff} 

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:

3 The operation x || y corresponds to
the operation x | y, except that y is
evaluated only if x is false.

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} 
¢蛋碎的人ぎ生 2024-08-27 01:03:03

你需要

if (title == "User greeting" || title == "User name") {do stuff};

you need

if (title == "User greeting" || title == "User name") {do stuff};
↙温凉少女 2024-08-27 01:03:03

条件或运算符是 ||:

if (expr1 || expr2) {do stuff}

if (title == "User greeting" || title == "User name") {do stuff}

条件(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 ||:

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.

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 .

紅太極 2024-08-27 01:03:03

或者是||

并且是&&

更改问题的更新:

您需要在 if 语句的每个逻辑部分中指定要比较的内容。

if (title == "User greeting" || title == "User name") 
{
    // do stuff
}

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 (title == "User greeting" || title == "User name") 
{
    // do stuff
}
南街九尾狐 2024-08-27 01:03:03

在 C#9 中,我们现在可以使用模式组合符“or”、“and”和“not”作为“is”条件检查中的运算符。

请参阅此内容了解详细信息。

所以你的代码

if (title == "User greeting" || "User name") {do stuff}

……可以直接写成:

if(title is "User greeting" or "User name") {do stuff}

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 ...

if (title == "User greeting" || "User name") {do stuff}

... could be directly written as:

if(title is "User greeting" or "User name") {do stuff}
小傻瓜 2024-08-27 01:03:03

请参阅 C# 运算符,了解 C# 运算符,包括 OR(|) |

See C# Operators for C# operators including OR which is ||

生来就爱笑 2024-08-27 01:03:03

这是错误的原因:

if (title == "User greeting" || "User name") {do stuff};

因为它的意思是

如果标题等于字符串“用户问候语”

或只是“用户名”(而不是如果标题等于字符串“用户名”)。 or 之后的部分就像写

if ("User name")

c# 不知道该怎么办。它无法弄清楚如何从“用户名”中获取布尔值

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"

穿透光 2024-08-27 01:03:03

格式为 if

if (this OR that) 

thisthat 是表达式而不是值。 title == "aaaaa" 是一个有效的表达式。另外,OR 在 C# 中不是有效的构造,您必须使用 ||

In the format for 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 ||.

没有心的人 2024-08-27 01:03:03

您应该使用 equal

if (title.Equals("User greeting") || title.Equals("User name")) 
{ do stuff }

You should use equal like

if (title.Equals("User greeting") || title.Equals("User name")) 
{ do stuff }
两人的回忆 2024-08-27 01:03:03

OR 用作“||”

 if(expr1 || expr2)
 {
    do something
 }

OR is used as "||"

 if(expr1 || expr2)
 {
    do something
 }
厌味 2024-08-27 01:03:03

您可以同时使用 &&或||

if (title == "User greeting" && title == "User name") {do stuff}

You can use both && or ||

if (title == "User greeting" && title == "User name") {do stuff}
千寻… 2024-08-27 01:03:03

那也是我的代码。我就这样解决了。

if ((title == "用户问候语") || (title == "用户名")) {do stuff}

That was my code too. I solved it like that.

if ((title == "User greeting") || (title == "User name")) {do stuff}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文