Home  >  Article  >  Backend Development  >  What is the difference between | and || or operator in C#?

What is the difference between | and || or operator in C#?

PHPz
PHPzforward
2023-08-25 18:29:231131browse

| 之间有什么区别?和||或者 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.

Example

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();
   }
}

Output

Value of c is 7
Here the values are converted to binary
4−−100
3−−011
Output 7 −−111

Example 2

is translated as:

Example 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();
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:BigInteger class in C#Next article:BigInteger class in C#