Converting Boolean to Strings in C#
In C#, a boolean data type can only have two values: true
or false
. These values are often used to represent the result of a comparison or a logical operation. While working with boolean values,
C# provides multiple ways to convert boolean values to strings. the approach is using the ToString()
method, which is available for boolean data types.
using System;
class Program
{
static void Main()
{
bool isTrue = true;
bool isFalse = false;
// Using ToString() method
string trueString = isTrue.ToString();
string falseString = isFalse.ToString();
// Displaying the results
Console.WriteLine("True as String: " + trueString);
Console.WriteLine("False as String: " + falseString);
}
}
In this program, we have declared two boolean variables (isTrue
and isFalse
) and used the ToString()
method to convert them into string representations.
Output:
True as String: True
False as String: False
Comments (0)