| 运算符
| 运算符计算其操作数的逻辑或。x的结果| 如果x或y的评估结果为true,则y为true。否则,结果为假。
| 即使左侧操作数的计算结果为true,运算符也会计算两个操作数,因此无论右侧操作数的值如何,运算结果均为true。
|| 运算符
条件逻辑或运算符||,也称为“短路”逻辑或运算符,计算其操作数的逻辑或。
x ||的结果 如果x或y的评估结果为true,则y为true。否则,结果为假。如果x评估为true,则不评估y。
class Program {
static void Main(string[] args){
int a = 4;
int b = 3;
int c = 0;
c = a | b;
Console.WriteLine("Line 1 - Value of c is {0}", c);
Console.ReadLine();
}
}输出结果
Value of c is 7 Here the values are converted to binary 4−−100 3−−011 Output 7 −−111
static void Main(string[] args){
int a = 4;
int b = 3;
int c = 7;
if (a > b || b > c){
System.Console.WriteLine("a is largest");
} else {
System.Console.WriteLine("a is not largest");
}
Console.ReadLine();
}输出结果
a is largest
在上面的示例中,条件之一返回true,因此它永远不会打扰检查下一个条件。