C# Logical Operators - Complete Guide
Introduction to Logical Operators
Logical operators in C# are used to combine or modify boolean expressions and produce a single boolean result. They are commonly applied in conditional statements to build more complex decision-making logic.
C# provides three main logical operators: AND (&&), OR (||), and NOT (!). These operators also support short-circuit evaluation, which can improve performance and help prevent runtime errors when evaluating expressions.
Basic Logical Operators
The primary logical operators in C# are used for boolean operations and control flow. They are typically used in if statements, loops, and conditional expressions.
using System;
namespace LogicalOperatorsExample
{
class Program
{
static void Main(string[] args)
{
bool trueValue = true;
bool falseValue = false;
// Logical AND (&&) - true only if both are true
Console.WriteLine($"true && true: {trueValue && trueValue}");
Console.WriteLine($"true && false: {trueValue && falseValue}");
Console.WriteLine($"false && false: {falseValue && falseValue}");
// Logical OR (||) - true if at least one is true
Console.WriteLine($"true || true: {trueValue || trueValue}");
Console.WriteLine($"true || false: {trueValue || falseValue}");
Console.WriteLine($"false || false: {falseValue || falseValue}");
// Logical NOT (!) - negates the value
Console.WriteLine($"!true: {!trueValue}");
Console.WriteLine($"!false: {!falseValue}");
// Complex expression example
bool a = true, b = false, c = true;
bool complexResult = (a && b) || (c && !b);
Console.WriteLine($"(a && b) || (c && !b): {complexResult}");
// Using logical operators with comparisons
int x = 10, y = 5, z = 15;
bool comparisonResult = (x > y) && (y < z) && (x != z);
Console.WriteLine($"(x > y) && (y < z) && (x != z): {comparisonResult}");
}
}
}
true && true: True true && false: False false && false: False true || true: True true || false: True false || false: False !true: False !false: True (a && b) || (c && !b): True (x > y) && (y < z) && (x != z): True
Short-Circuit Evaluation
C# logical operators && and || use short-circuit evaluation, meaning the second operand is only evaluated if necessary. This improves efficiency and avoids unnecessary method calls or potential exceptions.
Short-circuiting is especially useful for null checks and safe access to objects or arrays before performing further operations.
using System;
namespace ShortCircuitExample
{
class Program
{
static void Main(string[] args)
{
// Short-circuit AND (&&)
Console.WriteLine("Short-circuit AND example:");
bool result1 = false && SomeMethod(); // SomeMethod() not called
Console.WriteLine($"result1: {result1}");
bool result2 = true && SomeMethod(); // SomeMethod() called
Console.WriteLine($"result2: {result2}");
// Short-circuit OR (||)
Console.WriteLine("\nShort-circuit OR example:");
bool result3 = true || SomeMethod(); // SomeMethod() not called
Console.WriteLine($"result3: {result3}");
bool result4 = false || SomeMethod(); // SomeMethod() called
Console.WriteLine($"result4: {result4}");
// Practical null check example
string text = null;
if (text != null && text.Length > 0)
{
Console.WriteLine("Text has content");
}
else
{
Console.WriteLine("Text is null or empty");
}
// Safe array access
int[] numbers = null;
int index = 5;
if (numbers != null && index < numbers.Length && numbers[index] > 0)
{
Console.WriteLine("Valid array element");
}
else
{
Console.WriteLine("Invalid array access");
}
}
static bool SomeMethod()
{
Console.WriteLine("SomeMethod() was called");
return true;
}
}
}
Short-circuit AND example: result1: False SomeMethod() was called result2: True Short-circuit OR example: result3: True SomeMethod() was called result4: True Text is null or empty Invalid array access