Boolean Structure

Namespace: System.Runtime

Represents a Boolean value that is either true or false.

Syntax

public struct Boolean

Remarks

The Boolean structure is a fundamental type in the .NET Framework, representing logical truth values. It is used extensively in conditional statements, loops, and logical operations.

The Boolean value can be obtained through comparison operations, logical operators, or by converting string representations like "True" and "False".

Fields

True
Represents the Boolean value true. This is a static field.
False
Represents the Boolean value false. This is a static field.

Methods

CompareTo

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.

public int CompareTo(bool value)

Parameters:

Name Type Description
value bool The Boolean value to compare with the current instance.

Returns: A 32-bit signed integer that indicates the relative order of the objects being compared. Less than zero: value is greater than the current instance. Zero: value is equal to the current instance. Greater than zero: value is less than the current instance.

Equals

Determines whether the specified object is equal to the current object.

public bool Equals(bool obj)

Parameters:

Name Type Description
obj bool The object to compare with the current object.

Returns: true if the specified object is equal to the current object; otherwise, false.

GetHashCode

Returns the hash code for the current instance.

public int GetHashCode()

Returns: A hash code for the current instance.

ToString

Converts the value of the current Boolean instance to its equivalent string representation ("True" or "False").

public string ToString()

Returns: "True" or "False".

Static Methods

Parse

Converts the specified string representation to its Boolean equivalent.

public static bool Parse(string value)

Parameters:

Name Type Description
value string A string that contains the representation of a Boolean value to convert.

Returns: The Boolean value equivalent to the string representation.

Throws: ArgumentNullException if value is null. FormatException if value is not "True" or "False" (case-insensitive).

TryParse

Converts the specified string representation to its Boolean equivalent. A return value indicates whether the conversion succeeded or failed.

public static bool TryParse(string value, out bool result)

Parameters:

Name Type Description
value string A string that contains the representation of a Boolean value to convert.
result out bool When this method returns, contains the Boolean value equivalent to the string representation if the conversion succeeded, or a default value of false otherwise.

Returns: true if the value parameter was converted successfully; otherwise, false.

Example


using System;

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

        Console.WriteLine($"Is operation complete? {isComplete}");
        Console.WriteLine($"Are there errors? {hasErrors}");

        // Converting from string
        string status = "True";
        if (bool.TryParse(status, out bool parsedStatus))
        {
            Console.WriteLine($"Parsed status: {parsedStatus}");
        }
        else
        {
            Console.WriteLine("Failed to parse status.");
        }

        // Comparisons
        if (isComplete && !hasErrors)
        {
            Console.WriteLine("Operation succeeded.");
        }
        else
        {
            Console.WriteLine("Operation encountered issues.");
        }
    }
}