| |之间有什么区别?和|| 或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 送给学长毕业祝福语简短