Convert.ToBoolean Method (String)

Updated: May 2009

Converts the specified string representation of a logical value to its Boolean equivalent.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static bool ToBoolean(
	string value
)

Parameters

value
Type: System.String

A string that contains the value of either Boolean.TrueString or Boolean.FalseString.

Return Value

Type: System.Boolean
true if value equals TrueString, or false if value equals FalseString or null.

ExceptionCondition
FormatException

value is not equal to TrueString or FalseString.

For a successful conversion to occur, the value parameter must equal either Boolean.TrueString, a constant whose value is True, Boolean.FalseString, a constant whose value is False, or it must be null. In comparing value with Boolean.TrueString and Boolean.FalseString, the method ignores case as well as leading and trailing white space.

If you prefer not to handle an exception if the conversion fails, you can call the Boolean.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.

The following example uses the Convert.ToBoolean(String) method to convert various strings to Boolean values.

using System;

public class BooleanConversion
{
   public static void Main()
   {
      ConvertToBoolean(null);
      ConvertToBoolean(String.Empty);
      ConvertToBoolean("true");
      ConvertToBoolean("TrueString");
      ConvertToBoolean("False");
      ConvertToBoolean("    false    ");
      ConvertToBoolean("-1");
      ConvertToBoolean("0");
   }

   private static void ConvertToBoolean(string value)
   {
      try
      {
         Console.WriteLine("Converted '{0}' to {1}.", value,  
                           Convert.ToBoolean(value));
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to convert '{0}' to a Boolean.", value);
      }
   }
}
// The example displays the following output to the console: 
//       Converted '' to False. 
//       Unable to convert '' to a Boolean. 
//       Converted 'true' to True. 
//       Unable to convert 'TrueString' to a Boolean. 
//       Converted 'False' to False. 
//       Converted '    false    ' to False. 
//       Unable to convert '-1' to a Boolean. 
//       Unable to convert '0' to a Boolean.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

May 2009

Expanded the Remarks section.

Customer feedback.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft