Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Boolean.ToString(IFormatProvider) Method in C#
The Boolean.ToString(IFormatProvider) method in C# converts a Boolean value to its equivalent string representation. This method accepts an IFormatProvider parameter, though Boolean values are not culture-sensitive and always return "True" or "False" regardless of the culture specified.
Syntax
Following is the syntax for the Boolean.ToString(IFormatProvider) method −
public string ToString(IFormatProvider provider);
Parameters
-
provider − An
IFormatProviderobject that provides culture-specific formatting information. This parameter is reserved but not used for Boolean values.
Return Value
Returns a string representation of the Boolean value. The method returns "True" for true values and "False" for false values, regardless of the culture specified.
Using ToString() with Different Cultures
Although Boolean values are not culture-sensitive, you can still pass different CultureInfo objects to maintain consistency in your code −
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));
}
}
The output of the above code is −
Value1 (Hashcode) = 1 Value1 (TypeCode) = Boolean Value2 (Hashcode) = 0 Value2 (TypeCode) = Boolean Value1 = True Value2 = False
Culture Independence Example
This example demonstrates that Boolean string representation remains the same across different cultures −
using System;
using System.Globalization;
public class Demo {
public static void Main(String[] args) {
bool val1 = true;
bool val2 = false;
// Different cultures
CultureInfo usCulture = new CultureInfo("en-US");
CultureInfo frenchCulture = new CultureInfo("fr-FR");
CultureInfo germanCulture = new CultureInfo("de-DE");
Console.WriteLine("US Culture:");
Console.WriteLine("Value1 = " + val1.ToString(usCulture));
Console.WriteLine("Value2 = " + val2.ToString(usCulture));
Console.WriteLine("\nFrench Culture:");
Console.WriteLine("Value1 = " + val1.ToString(frenchCulture));
Console.WriteLine("Value2 = " + val2.ToString(frenchCulture));
Console.WriteLine("\nGerman Culture:");
Console.WriteLine("Value1 = " + val1.ToString(germanCulture));
Console.WriteLine("Value2 = " + val2.ToString(germanCulture));
}
}
The output of the above code is −
US Culture: Value1 = True Value2 = False French Culture: Value1 = True Value2 = False German Culture: Value1 = True Value2 = False
Comparison with Other ToString() Overloads
| Method | Description | Usage |
|---|---|---|
ToString() |
Basic string conversion | Simple Boolean to string conversion |
ToString(IFormatProvider) |
Culture-aware conversion | Consistent API, though culture doesn't affect Boolean output |
Conclusion
The Boolean.ToString(IFormatProvider) method provides a culture-aware interface for converting Boolean values to strings, though the output remains constant as "True" or "False" regardless of culture. This method is useful for maintaining consistency in internationalized applications where other data types benefit from culture-specific formatting.
