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.