C# 中的 Boolean.ToString(IFormatProvider) 方法
C# 中的 Boolean.ToString() 方法用于将此实例的值转换为其等效的字符串表示形式。
语法
语法如下 −
public string ToString (IFormatProvider provider);
在上面,参数 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()); // changed the culture 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
广告