Boolean Struct

Represents a Boolean value (true or false).

public struct Boolean : IComparable, IConvertible, IEquatable<bool>

Description

The Boolean struct represents a truth value, which can be either true or false. In C#, the keyword bool is an alias for System.Boolean.

Boolean values are fundamental to programming, used in conditional statements, loops, and logical operations.

Remarks

The Boolean struct implements the IComparable, IConvertible, and IEquatable<bool> interfaces.

  • IComparable: Allows for comparison of boolean values (false is considered less than true).
  • IConvertible: Provides methods to convert the boolean value to other data types.
  • IEquatable<bool>: Enables equality comparison with other boolean values.

The default value for a Boolean is false.

Members

Constants

Name Description
False Represents the Boolean value false.
True Represents the Boolean value true.

Methods

Name Description
CompareTo(bool other) Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
Equals(object obj) Determines whether the specified object is equal to the current object.
Equals(bool other) Indicates whether the current object is equal to another object of the same type.
GetHashCode() Returns the hash code for the current instance.
GetTypeCode() Returns the type code for the current instance.
Parse(string value) Converts the specified string representation to its equivalent Boolean value.
ToString() Returns the string representation of the value of this instance.
ToString(IFormatProvider provider) Converts the value of this instance to its equivalent string representation using the specified format provider.
TryParse(string value, out bool result) Converts the string representation of a value to its Boolean equivalent. A return value indicates whether the conversion succeeded.

Example

using System;

public class BooleanExample
{
    public static void Main(string[] args)
    {
        bool isComplete = true;
        bool hasError = false;

        Console.WriteLine($"Is the task complete? {isComplete}");
        Console.WriteLine($"Are there any errors? {hasError}");

        if (isComplete && !hasError)
        {
            Console.WriteLine("Process completed successfully.");
        }
        else
        {
            Console.WriteLine("Process encountered issues.");
        }

        // Using Parse
        string trueString = "True";
        bool parsedBool = bool.Parse(trueString);
        Console.WriteLine($"Parsed '{trueString}': {parsedBool}");

        // Using TryParse
        string falseString = "false";
        bool tryParsedBool;
        if (bool.TryParse(falseString, out tryParsedBool))
        {
            Console.WriteLine($"TryParse '{falseString}': {tryParsedBool}");
        }
        else
        {
            Console.WriteLine($"TryParse failed for '{falseString}'.");
        }

        // Comparing booleans
        Console.WriteLine($"Is true == true? {Boolean.Equals(true, true)}");
        Console.WriteLine($"Is true.CompareTo(false) > 0? {true.CompareTo(false) > 0}");
    }
}