| |之间有什么区别?和|| 或C#中的运算符?
|运算符
|运算符计算其操作数的逻辑或。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
例子2
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,因此它永远不会打扰检查下一个条件。
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短