Converts string to Boolean
using System;
public class MainClass
{
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);
}
}
}
Related examples in the same category