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

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

PHPz
Release: 2023-08-25 18:29:23
forward
1211 people have browsed it

| 之间有什么区别?和||或者 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();
   }
}
Copy after login

Output

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

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

Output

a is largest
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template