Skip to main content
.NET Framework Class Library
Boolean..::.Parse Method

Updated: September 2010

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

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
Public Shared Function Parse ( _
	value As String _
) As Boolean
public static bool Parse(
	string value
)
public:
static bool Parse(
	String^ value
)
static member Parse : 
        value:string -> bool 

Parameters

value
Type: System..::.String
A string containing the value to convert.

Return Value

Type: System..::.Boolean
true if value is equivalent to the value of the Boolean..::.TrueString field; false if value is equivalent to the value of the Boolean..::.FalseString field.
Exceptions
ExceptionCondition
ArgumentNullException

value is nullNothingnullptra null reference (Nothing in Visual Basic).

FormatException

value is not equivalent to the value of the TrueString or FalseString field.

Remarks

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

Examples

The following example calls the Parse method to parse an array of strings. Note that the parse operation succeeds only if the string to be parsed is "True" (the value of the TrueString field) or "False" (the value of the FalseString field) in a case-insensitive comparison.


Module Example
   Public Sub Main()
      Dim values() As String = { Nothing, String.Empty, "True", "False", 
                                 "true", "false", "    true    ", "0", 
                                 "1", "-1", "string" }
      For Each value In values
         Try
            Dim flag As Boolean = Boolean.Parse(value)
            Console.WriteLine("'{0}' --> {1}", value, flag)
         Catch e As ArgumentException
            Console.WriteLine("Cannot parse a null string.")
         Catch e As FormatException
            Console.WriteLine("Cannot parse '{0}'.", value)
         End Try         
      Next                                     
   End Sub
End Module
' The example displays the following output:
'       Cannot parse a null string.
'       Cannot parse ''.
'       'True' --> True
'       'False' --> False
'       'true' --> True
'       'false' --> False
'       '    true    ' --> True
'       Cannot parse '0'.
'       Cannot parse '1'.
'       Cannot parse '-1'.
'       Cannot parse 'string'.


using System;

public class Example
{
   public static void Main()
   {
      string[] values = { null, String.Empty, "True", "False", 
                          "true", "false", "    true    ", "0", 
                          "1", "-1", "string" };
      foreach (var value in values) {
         try {
            bool flag = Boolean.Parse(value);
            Console.WriteLine("'{0}' --> {1}", value, flag);
         }
         catch (ArgumentException) {
            Console.WriteLine("Cannot parse a null string.");
         }   
         catch (FormatException) {
            Console.WriteLine("Cannot parse '{0}'.", value);
         }         
      }                                     
   }
}
// The example displays the following output:
//       Cannot parse a null string.
//       Cannot parse ''.
//       'True' --> True
//       'False' --> False
//       'true' --> True
//       'false' --> False
//       '    true    ' --> True
//       Cannot parse '0'.
//       Cannot parse '1'.
//       Cannot parse '-1'.
//       Cannot parse 'string'.

Version Information

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Change History

Date

History

Reason

September 2010

Revised extensively and replaced the example.

Customer feedback.

June 2010

Corrected return value information.

Customer feedback.

Microsoft is conducting an online survey to understand your opinion of the MSDN Web site. If you choose to participate, the online survey will be presented to you when you leave the MSDN Web site.

Would you like to participate?