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

CsharpServer Side ProgrammingProgramming

| Operator

The | 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 even if the left-hand operand evaluates to true, so that the operation result is true regardless of the value of the right-hand operand.

|| Operator

The conditional logical OR operator ||, also known as the "short−circuiting" logical OR 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. 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);
}
}

## Output

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

## 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");
}
}

## Output

a is largest

Here in the above example one of the condition return true so it never bothers to check the next condition.

Published on 05-Nov-2020 13:45:40