Mono Class Library: System.Convert Overview | Members

System.Convert.ToBoolean Method

Converts a string to a bool .

public static bool ToBoolean (string value)

Parameters

value
The string to be converted.

Returns

true if value equals bool.TrueString; false if value equals bool.FalseString.

Exceptions

TypeReason
ArgumentNullExceptionvalue is a null reference.
FormatExceptionvalue is not equal to bool.TrueString or bool.FalseString.

Remarks

Documentation for this section has not yet been entered.

Example

The following example demonstrates converting string values to bool values.

C# Example

using System;
class ConvertBoolTest {
    static public void Main() {
        string string0 = Boolean.TrueString;
        string string1 = Boolean.FalseString; 
        string string2 = "foo"; //This is an invalid Boolean.
        bool bool0 = Convert.ToBoolean(string0);
        bool bool1 = Convert.ToBoolean(string1);
        Console.WriteLine("(string) {0} as bool = {1}",string0,bool0);
        Console.WriteLine("(string) {0} as bool = {1}",string1,bool1);
        bool bool2 = Convert.ToBoolean(string2); //Throws an exception.
        Console.WriteLine("(string) {0} as bool = {1}",string2,bool2);
    }
}

The output is

Example

(string) True as bool = True
(string) False as bool = False
Unhandled Exception: System.FormatException: String was not recognized as a valid Boolean.
      at System.Boolean.Parse(String value)
      at Convert.ToBoolean(String value)
      at ConvertBoolTest.Main() in C:\ECMAExamples\ConvertString.cs:line 12

Requirements

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