C# Convert ToBoolean(String)
Description
Convert ToBoolean(String)
converts the specified string
representation of a logical value to its Boolean equivalent.
Syntax
Convert.ToBoolean(String)
has the following syntax.
public static bool ToBoolean(
string value
)
Parameters
Convert.ToBoolean(String)
has the following parameters.
value
- A string that contains the value of either Boolean.TrueString or Boolean.FalseString.
Returns
Convert.ToBoolean(String)
method returns true if value equals TrueString, or false if value equals FalseString or
null.
Example
The following example uses the Convert.ToBoolean(String) method to convert various strings to Boolean values.
using System;/* w w w. java 2 s . c o m*/
public class BooleanConversion
{
public static void Main()
{
ConvertToBoolean(null);
ConvertToBoolean(String.Empty);
ConvertToBoolean("true");
ConvertToBoolean("False");
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 code above generates the following result.