System.Boolean.Parse Method

Converts the specified string representation of a logical value to its bool equivalent, or throws an exception if the string is not equal to the value of bool.TrueString or bool.FalseString.

Syntax

public static bool Parse (string value)

Parameters

value
A string containing the value to convert.

Returns

true if value is equal to the value of the bool.TrueString field; false if value is equal to the value of the bool.FalseString field.

Exceptions

TypeReason
ArgumentNullException value is a null reference.
FormatException value is not equivalent to either bool.TrueString or bool.FalseString.

Remarks

The value parameter, optionally preceded or trailed by white space, must contain a string that is equal to the value of either the bool.TrueString or bool.FalseString field; otherwise, a FormatException is thrown. The comparison is ordinal and case-insensitive.

You can call the bool.TryParse(string, Boolean@) method to parse a string without having to handle the FormatException exception that is thrown if the parsing operation fails.

Example

The following example demonstrates the bool.Parse(string) method.

C# Example

using System;
public class BoolParse {
   public static void Main() {
      Boolean b = Boolean.Parse("  true  ");
      Console.WriteLine("\"  true  \" parses to \"{0}\".", b);
   }
}
   

The output is

" true " parses to "True".

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0