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() Method in C#
The Boolean.ToString() method in C# converts a boolean value to its equivalent string representation. It returns "True" for true values and "False" for false values.
Syntax
Following is the syntax −
public override string ToString();
Return Value
The method returns a string that represents the current boolean value −
-
Returns
"True"if the boolean value istrue -
Returns
"False"if the boolean value isfalse
Using Boolean.ToString() with True Value
Example
using System;
public class Demo {
public static void Main() {
bool b = true;
string res = b.ToString();
Console.WriteLine("Boolean value: " + b);
Console.WriteLine("String representation: " + res);
Console.WriteLine("Type: " + res.GetType().Name);
}
}
The output of the above code is −
Boolean value: True String representation: True Type: String
Using Boolean.ToString() with False Value
Example
using System;
public class Demo {
public static void Main() {
bool b = false;
string res = b.ToString();
Console.WriteLine("Boolean value: " + b);
Console.WriteLine("String representation: " + res);
Console.WriteLine("Are they equal as strings? " + (res == "False"));
}
}
The output of the above code is −
Boolean value: False String representation: False Are they equal as strings? True
Practical Example with Multiple Boolean Values
Example
using System;
public class Demo {
public static void Main() {
bool[] booleans = { true, false, 5 > 3, 10 < 2 };
Console.WriteLine("Boolean to String Conversions:");
for (int i = 0; i < booleans.Length; i++) {
Console.WriteLine($"Value {i + 1}: {booleans[i]} -> "{booleans[i].ToString()}"");
}
// Demonstrate case sensitivity
bool check = true;
Console.WriteLine("\nCase sensitivity check:");
Console.WriteLine("ToString() result: " + check.ToString());
Console.WriteLine("Equals 'true': " + (check.ToString() == "true"));
Console.WriteLine("Equals 'True': " + (check.ToString() == "True"));
}
}
The output of the above code is −
Boolean to String Conversions: Value 1: True -> "True" Value 2: False -> "False" Value 3: True -> "True" Value 4: False -> "False" Case sensitivity check: ToString() result: True Equals 'true': False Equals 'True': True
Key Points
-
The
ToString()method returns capitalized strings: "True" and "False" (not "true" or "false") -
The method is inherited from the
Objectclass and overridden in theBooleanstruct -
It's commonly used for displaying boolean values in user interfaces or logging
-
The returned string can be used in string comparisons but remember the exact casing
Conclusion
The Boolean.ToString() method provides a simple way to convert boolean values to their string representations. It returns "True" for true values and "False" for false values, with proper capitalization that should be considered when performing string comparisons.
