Convert.ToBoolean(String, IFormatProvider) Method in C#

The Convert.ToBoolean(String, IFormatProvider) method in C# converts a string representation to an equivalent Boolean value using culture-specific formatting information. This method accepts only specific string values: "True", "False", or their equivalents in different cultures.

Syntax

Following is the syntax −

public static bool ToBoolean(string value, IFormatProvider provider);

Parameters

  • value − A string containing the value of either TrueString or FalseString.

  • provider − An object that supplies culture-specific formatting information (this parameter is ignored for Boolean conversions).

Return Value

Returns true if value equals TrueString, or false if value equals FalseString.

Using Convert.ToBoolean with Culture Information

Example

using System;
using System.Globalization;

public class Demo {
   public static void Main() {
      CultureInfo cultures = new CultureInfo("en-US");
      String str = "true";
      Console.WriteLine("Converting string to boolean...");
      bool res = Convert.ToBoolean(str, cultures);
      Console.WriteLine("Result: {0}", res);
      
      // Test with "false"
      String str2 = "false";
      bool res2 = Convert.ToBoolean(str2, cultures);
      Console.WriteLine("Result: {0}", res2);
   }
}

The output of the above code is −

Converting string to boolean...
Result: True
Result: False

Case-Insensitive Conversion

Example

using System;
using System.Globalization;

public class Program {
   public static void Main() {
      CultureInfo provider = new CultureInfo("en-US");
      
      string[] values = {"TRUE", "False", "true", "FALSE"};
      
      Console.WriteLine("String to Boolean conversion:");
      foreach (string value in values) {
         try {
            bool result = Convert.ToBoolean(value, provider);
            Console.WriteLine("'{0}' -> {1}", value, result);
         }
         catch (FormatException) {
            Console.WriteLine("'{0}' -> Invalid format", value);
         }
      }
   }
}

The output of the above code is −

String to Boolean conversion:
'TRUE' -> True
'False' -> False
'true' -> True
'FALSE' -> False

Handling Invalid Values

Example

using System;
using System.Globalization;

public class Program {
   public static void Main() {
      CultureInfo provider = new CultureInfo("fr-FR");
      
      string[] testValues = {"true", "yes", "1", "0", ""};
      
      Console.WriteLine("Testing various string values:");
      foreach (string value in testValues) {
         try {
            bool result = Convert.ToBoolean(value, provider);
            Console.WriteLine("'{0}' -> {1}", value, result);
         }
         catch (FormatException ex) {
            Console.WriteLine("'{0}' -> Error: {1}", value, ex.Message);
         }
      }
   }
}

The output of the above code is −

Testing various string values:
'true' -> True
'yes' -> Error: String 'yes' was not recognized as a valid Boolean.
'1' -> Error: String '1' was not recognized as a valid Boolean.
'0' -> Error: String '0' was not recognized as a valid Boolean.
'' -> Error: String '' was not recognized as a valid Boolean.

Key Rules

  • Only accepts "True" and "False" (case-insensitive) as valid input strings.

  • The IFormatProvider parameter is ignored for Boolean conversions.

  • Throws FormatException for any string other than valid Boolean representations.

  • Leading and trailing whitespace is ignored during conversion.

Conclusion

The Convert.ToBoolean(String, IFormatProvider) method provides a strict way to convert strings to Boolean values, accepting only "True" and "False" (case-insensitive). While the IFormatProvider parameter exists for consistency with other conversion methods, it has no effect on Boolean conversions.

Updated on: 2026-03-17T07:04:35+05:30

483 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements