Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between | and || operators in c#?
The || is called logical OR operator and | is called bitwise logical OR operator. The key difference between them lies in how they evaluate expressions and when they stop execution.
Syntax
Both operators have similar syntax −
bool_exp1 || bool_exp2 // Logical OR (short-circuit) bool_exp1 | bool_exp2 // Bitwise OR (always evaluates both)
Key Differences
| Logical OR (||) | Bitwise OR (|) |
|---|---|
| Short-circuit evaluation - stops if first expression is true | Always evaluates both expressions |
| More efficient for boolean operations | Less efficient for boolean operations |
| Used primarily with boolean expressions | Used for bitwise operations and boolean expressions |
| Second expression may not execute | Both expressions always execute |
Using Logical OR (||) - Short-Circuit Evaluation
The || operator uses short-circuit evaluation. If the first condition is true, the second condition is not evaluated because the result is already determined −
using System;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
if(Condition1() || Condition2()) {
Console.WriteLine("Logical OR If Condition Executed");
}
}
static bool Condition1() {
Console.WriteLine("Condition 1 executed");
return true;
}
static bool Condition2() {
Console.WriteLine("Condition 2 executed");
return true;
}
}
}
The output of the above code is −
Condition 1 executed Logical OR If Condition Executed
Notice that Condition2() is never called because Condition1() returned true.
Using Bitwise OR (|) - Always Evaluates Both
The | operator always evaluates both expressions regardless of the first expression's result −
using System;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
if(Condition1() | Condition2()) {
Console.WriteLine("Bitwise OR If Condition Executed");
}
}
static bool Condition1() {
Console.WriteLine("Condition 1 executed");
return true;
}
static bool Condition2() {
Console.WriteLine("Condition 2 executed");
return true;
}
}
}
}
The output of the above code is −
Condition 1 executed Condition 2 executed Bitwise OR If Condition Executed
Both conditions are executed even though the first one returned true.
Bitwise OR with Integer Values
The | operator is also used for bitwise operations on integer types −
using System;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
int a = 5; // Binary: 101
int b = 3; // Binary: 011
int result = a | b; // Binary: 111 = 7
Console.WriteLine($"5 | 3 = {result}");
Console.WriteLine($"Binary: 101 | 011 = {Convert.ToString(result, 2)}");
}
}
}
The output of the above code is −
5 | 3 = 7 Binary: 101 | 011 = 111
When to Use Which Operator
-
Use
||for boolean logic where you want efficient short-circuit evaluation. -
Use
|for bitwise operations on integer types or when you need both expressions to execute. -
In most boolean conditions,
||is preferred for better performance.
Conclusion
The || operator provides short-circuit evaluation for boolean expressions, making it more efficient by stopping evaluation once the result is determined. The | operator always evaluates both operands and is primarily used for bitwise operations, though it can also work with boolean values when you need both expressions to execute.
