Boolean.ToString(IFormatProvider) Method in C#


The Boolean.ToString() method in C# is used to convert the value of this instance to its equivalent string representation.

Syntax

The syntax is as follows −

public string ToString (IFormatProvider provider);

Above, the parameter provider is an IFormatProvider object.

Example

Let us now see an example −

 Live Demo

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));
   }
}

Output

This will produce the following output −

Value1  (Hashcode) = 1
Value1 (TypeCode) = Boolean
Value2  (Hashcode) = 0
Value2 (TypeCode) = Boolean
Value1 = True
Value2 = False

Example

Let us now see another example −

 Live Demo

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));
   }
}

Output

This will produce the following output −

Value1  (Hashcode) = 1
Value1 (TypeCode) = Boolean
Value2  (Hashcode) = 0
Value2 (TypeCode) = Boolean
Value1 = True
Value2 = False
raja
Published on 04-Dec-2019 10:16:29
Advertisements