C#中的Boolean.ToString()方法用于将该实例的值转换为其等效的字符串表示形式。
语法如下-
public string ToString (IFormatProvider provider);
上面的参数提供者是IFormatProvider对象。
现在让我们看一个例子-
using System; using System.Globalization; public class Demo { public static void Main(String[] args) { bool val1 = true; bool val2 = false; Console.WriteLine("Value1 (Hashcode) = "+val1.GetHashCode()); Console.WriteLine("Value1 (TypeCode) = "+val1.GetTypeCode()); Console.WriteLine("Value2 (Hashcode) = "+val2.GetHashCode()); Console.WriteLine("Value2 (TypeCode) = "+val2.GetTypeCode()); CultureInfo provider = new CultureInfo("en-us"); Console.WriteLine("Value1 = "+val1.ToString(provider)); Console.WriteLine("Value2 = "+val2.ToString(provider)); } }
输出结果
这将产生以下输出-
Value1 (Hashcode) = 1 Value1 (TypeCode) = Boolean Value2 (Hashcode) = 0 Value2 (TypeCode) = Boolean Value1 = True Value2 = False
现在让我们来看另一个示例-
using System; using System.Globalization; public class Demo { public static void Main(String[] args) { bool val1 = true; bool val2 = false; Console.WriteLine("Value1 (Hashcode) = "+val1.GetHashCode()); Console.WriteLine("Value1 (TypeCode) = "+val1.GetTypeCode()); Console.WriteLine("Value2 (Hashcode) = "+val2.GetHashCode()); Console.WriteLine("Value2 (TypeCode) = "+val2.GetTypeCode()); //改变了文化 CultureInfo provider = new CultureInfo("fr-FR"); Console.WriteLine("Value1 = "+val1.ToString(provider)); Console.WriteLine("Value2 = "+val2.ToString(provider)); } }
输出结果
这将产生以下输出-
Value1 (Hashcode) = 1 Value1 (TypeCode) = Boolean Value2 (Hashcode) = 0 Value2 (TypeCode) = Boolean Value1 = True Value2 = False