What are Booleans types in C#?

The bool type in C# represents Boolean values and can store only two possible values: true and false. The bool keyword is an alias for the System.Boolean structure in the .NET framework.

Boolean variables are commonly used in conditional statements, loops, and logical operations to control program flow based on true/false conditions.

Syntax

Following is the syntax for declaring Boolean variables −

bool variableName = true;  // or false
bool variableName;         // defaults to false

Basic Boolean Declaration and Assignment

Example

using System;

public class BooleanDemo {
    static void Main() {
        bool isTrue = true;
        bool isFalse = false;
        bool defaultValue;  // defaults to false
        
        Console.WriteLine("isTrue: " + isTrue);
        Console.WriteLine("isFalse: " + isFalse);
        Console.WriteLine("defaultValue: " + defaultValue);
    }
}

The output of the above code is −

isTrue: True
isFalse: False
defaultValue: False

Using Boolean with Conditional Logic

Example

using System;

public class ConditionalDemo {
    static void Main() {
        bool val = true;
        int d = DateTime.Now.DayOfYear;
        val = (d % 2 == 0);
        
        if (val) {
            Console.WriteLine("Day of year: even number");
        } else {
            Console.WriteLine("Day of year: odd number");
        }
        
        Console.WriteLine("Current day of year: " + d);
    }
}

The output will vary based on the current date. Example output −

Day of year: odd number
Current day of year: 347

Boolean Operations and Comparisons

Example

using System;

public class BooleanOperations {
    static void Main() {
        int a = 10, b = 20;
        
        bool isEqual = (a == b);
        bool isGreater = (a > b);
        bool isLess = (a < b);
        bool logicalAnd = (a > 5) && (b < 30);
        bool logicalOr = (a > 15) || (b > 15);
        bool logicalNot = !(a == b);
        
        Console.WriteLine("a == b: " + isEqual);
        Console.WriteLine("a > b: " + isGreater);
        Console.WriteLine("a < b: " + isLess);
        Console.WriteLine("(a > 5) && (b < 30): " + logicalAnd);
        Console.WriteLine("(a > 15) || (b > 15): " + logicalOr);
        Console.WriteLine("!(a == b): " + logicalNot);
    }
}

The output of the above code is −

a == b: False
a > b: False
a < b: True
(a > 5) && (b < 30): True
(a > 15) || (b > 15): True
!(a == b): True

Key Properties of Boolean Type

  • Default value is false when declared without initialization.

  • Cannot be directly converted to numeric types − explicit conversion is required.

  • Commonly used with logical operators: && (AND), || (OR), ! (NOT).

  • Essential for control flow statements like if, while, and for loops.

Conclusion

The bool type in C# is fundamental for representing true/false conditions in programming. It serves as the foundation for conditional logic, comparisons, and control flow statements, making it essential for decision-making processes in C# applications.

Updated on: 2026-03-17T07:04:35+05:30

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements