Home > Article > Backend Development > What is the difference between | and || or operator in C#?
| Operator
| An operator computes the logical OR of its operands. The result of x | y is true if either x or y evaluates to true. Otherwise, the result is false.
The | operator evaluates both operands so that the result of the operation is true, regardless of the value of the right operand, even if the left operand evaluates to true.
|| Operator
The conditional logical OR operator ||, also known as the "short-circuit" logical OR operator, computes the logical OR of its operands.
If either x or y evaluates to true, then x || y evaluates to true. Otherwise, the result is false. If x evaluates to true, y is not evaluated.
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
In the above example, one of the conditions returns true, so it never checks the next condition.
The above is the detailed content of What is the difference between | and || or operator in C#?. For more information, please follow other related articles on the PHP Chinese website!