DevAcademia
C++C#CPythonJava
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction

Loading Cs tutorial…

Loading content
C# BasicsTopic 17 of 55
←PreviousPrevNextNext→

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.

Example
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}");
        }
    }
}
Output
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.

Example
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;
        }
    }
}
Output
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
Test your knowledge: C# Logical Operators - Complete Guide
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C# BasicsTopic 17 of 55
←PreviousPrevNextNext→