| |之间有什么区别?和|| 或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,因此它永远不会打扰检查下一个条件。
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿