要将指定的值转换为等效的布尔值,代码如下:
using System; using System.Globalization; public class Demo { public static void Main() { CultureInfo cultures = new CultureInfo("en-US"); String str = "true"; Console.WriteLine("Converted bool value..."); bool res = Convert.ToBoolean(str, cultures); Console.Write("{0}", res); } }
输出结果
这将产生以下输出-
Converted bool value... True
让我们看另一个例子-
using System; using System.Globalization; public class Demo { public static void Main() { CultureInfo cultures = new CultureInfo("es-ES", false); String str = "false"; Console.WriteLine("Converted bool value..."); bool res = Convert.ToBoolean(str, cultures); Console.Write("{0}", res); } }
输出结果
这将产生以下输出-
Converted bool value... False