在C#中,如何指定OR:
if(this OR that) {do the other thing}
Run Code Online (Sandbox Code Playgroud)
我在帮助中找不到它.
更新:
我的代码是:
if (title == "User greeting" || "User name") {do stuff}
Run Code Online (Sandbox Code Playgroud)
我的错误是:
错误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
And*_*est 72
|| 是C#中的条件OR运算符
你可能很难找到它,因为很难找到你不知道名字的东西.下次尝试在Google上搜索"C#Operators"并查看逻辑运算符.
我的代码是:
Run Code Online (Sandbox Code Playgroud)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
你需要这样做:
if (title == "User greeting" || title == "User name") {do stuff};
Run Code Online (Sandbox Code Playgroud)
OR运算符以相同的方式计算两侧的表达式.在您的示例中,您正在对表达式title == "User greeting"(bool)和表达式"User name"(字符串)进行操作.如果没有强制转换或转换,这些不能直接组合,这就是您收到错误的原因.
此外,值得注意的是||操作员使用"短路评估".这意味着如果第一个表达式求值为true,则不计算第二个表达式,因为它不必是 - 最终结果将始终为true.有时您可以在优化期间利用这一点.
最后一个快速注释 - 我经常使用嵌套括号编写我的条件,如下所示:
if ((title == "User greeting") || (title == "User name")) {do stuff};
Run Code Online (Sandbox Code Playgroud)
这样我就可以控制优先级,而不必担心操作的顺序.这可能有点过头了,但是当逻辑变得复杂时它尤其有用.
wom*_*omp 11
OR运算符是双管道:
||
Run Code Online (Sandbox Code Playgroud)
所以它看起来像:
if (this || that)
{
//do the other thing
}
Run Code Online (Sandbox Code Playgroud)
编辑: 更新的尝试不起作用的原因是逻辑运算符必须分隔有效的C#表达式.表达式具有操作数,运算符和运算符具有优先顺序.
在您的情况下,首先评估==运算符.这意味着您的表达式被评估为(title == "User greeting") || "User name".|| 接下来评估.自|| 要求每个操作数都是一个布尔表达式,它会失败,因为你的操作数是字符串.
使用两个单独的布尔表达式将确保您的||运算符正常工作.
title == "User greeting" || title == "User name"
Run Code Online (Sandbox Code Playgroud)
你需要
if (title == "User greeting" || title == "User name") {do stuff};
Run Code Online (Sandbox Code Playgroud)
为了完整,|| 和&&是|的条件版本 和&运营商.
在ECMA C#语言规范中的引用是在这里.
从规格:
3操作x || y对应于操作x | y,除了仅在x为假时才计算y.
在|版本中,双方都进行了评估.
条件版本短路评估,因此允许代码如下:
if (x == null || x.Value == 5)
// Do something
Run Code Online (Sandbox Code Playgroud)
或者(没有双关语意)使用你的例子:
if (title == "User greeting" || title == "User name")
// {do stuff}
Run Code Online (Sandbox Code Playgroud)
或者是||
并且是 &&
已更改问题的更新:
您需要在if语句的每个逻辑部分中指定要比较的内容.
if (title == "User greeting" || title == "User name")
{
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
条件或运算符是||:
if (expr1 || expr2) {do stuff}
if (title == "User greeting" || title == "User name") {do stuff}
Run Code Online (Sandbox Code Playgroud)
条件(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.
在 C#9 中,我们现在可以使用模式组合符“or”、“and”和“not”作为“is”条件检查中的运算符。
请参阅此了解更多信息。
所以你的代码...
if (title == "User greeting" || "User name") {do stuff}
Run Code Online (Sandbox Code Playgroud)
...可以直接写为:
if(title is "User greeting" or "User name") {do stuff}
Run Code Online (Sandbox Code Playgroud)