System.Boolean.Parse

Namespace: System

Type: Boolean

Method: Parse

Converts the string representation of a number to its equivalent 32-bit signed integer. A return value indicates whether the conversion succeeded or failed.

Overloads

public static bool Parse(string value)

Converts the string representation of a number to its equivalent 32-bit signed integer.

Parameters

Return Value

If the conversion is successful, returns the 32-bit signed integer equivalent of the value parameter. Otherwise, returns 0.

Exceptions

FormatException: value is not in a supported format.

ArgumentNullException: value is null.

Remarks

The Parse method converts a string representation of a boolean value to its Boolean equivalent. The string can be "True" or "False" (case-insensitive). If the string does not represent a valid boolean value, a FormatException is thrown.

Example


string trueString = "True";
bool isTrue = Boolean.Parse(trueString); // isTrue will be true

string falseString = "false";
bool isFalse = Boolean.Parse(falseString); // isFalse will be false

try
{
    string invalidString = "yes";
    Boolean.Parse(invalidString);
}
catch (FormatException ex)
{
    Console.WriteLine($"Error: {ex.Message}"); // Output: Error: Input string was not in a correct format.
}