Mohanapriya R Mohanapriya R
Updated date Jul 10, 2024
In this article, we will explore the various methods for converting boolean values to strings.
  • 1.7k
  • 0
  • 0

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)

There are no comments. Be the first to comment!!!