C# Boolean Parse

Description

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

Syntax

Boolean.Parse has the following syntax.


public static bool Parse(
  string value
)

Parameters

Boolean.Parse has the following parameters.

  • value - A string containing the value to convert.

Returns

Boolean.Parse method returns true if value is equal to the value of the Boolean.TrueString field; false if value is equal to the value of the Boolean.FalseString field.

Example

The following example calls the Parse method to parse a string and get the boolean value.


/*www .  java 2  s  . c  om*/
using System;

public class Example
{
   public static void Main()
   {
      string[] values = { "True", "False", "true", "false", " true ",};
      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 code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version