Sai A Sai A
Updated date Apr 13, 2024
In this blog, we will learn how to convert Boolean to strings in C#, including ToString(), and Convert.ToString(), String.Format(), string interpolation, and Boolean.ToString().

Method 1: Using ToString() Method

The ToString() method is a built-in method in C# that allows you to convert an object to its string representation. Since Boolean values are objects in C#, you can use the ToString() method to convert a Boolean value to a string. 

bool boolValue = true;
string stringValue = boolValue.ToString();
Console.WriteLine("Boolean to String using ToString(): " + stringValue);

Output:

Boolean to String using ToString(): True

In this method, we simply call the ToString() method on the Boolean value, which returns a string representation of the Boolean value. If the Boolean value is true, the ToString() method returns "True", and if the Boolean value is false, it returns "False".

Method 2: Using Convert.ToString() Method

The Convert class in C# provides various methods for converting values from one type to another, including converting Boolean values to strings. The Convert.ToString() method can be used to convert a Boolean value to a string. 

bool boolValue = false;
string stringValue = Convert.ToString(boolValue);
Console.WriteLine("Boolean to String using Convert.ToString(): " + stringValue);

Output:

Boolean to String using Convert.ToString(): False

In this method, we use the Convert.ToString() method and pass the Boolean value as an argument to convert it to a string. The Convert.ToString() method returns "False" if the Boolean value is false, and "True" if the Boolean value is true.

Method 3: Using String.Format() Method

The String.Format() method in C# allows you to format strings by replacing placeholders with values. You can use this method to convert a Boolean value to a string by providing a format string with a placeholder for the Boolean value. 

bool boolValue = true;
string stringValue = String.Format("{0}", boolValue);
Console.WriteLine("Boolean to String using String.Format(): " + stringValue);

Output:

Boolean to String using String.Format(): True

In this method, we use the String.Format() method and provide a format string "{0}" with a placeholder for the Boolean value. The Boolean value is passed as an argument to the String.Format() method, and it replaces the placeholder in the format string with its string representation, which is "True" for a true Boolean value and "False" for a false Boolean value.

Method 4: Using String Interpolation

String interpolation is a feature introduced in C# 6.0 that allows you to embed expressions inside string literals. You can use string interpolation to directly convert a Boolean value to a string by embedding the Boolean value inside a string literal. 

bool boolValue = false;
string stringValue = $"{boolValue}";
Console.WriteLine("Boolean to String using String Interpolation: " + stringValue);

Output:

Boolean to String using String Interpolation: False

In this method, we use string interpolation by prefixing the string literal with a "$" symbol. Inside the string literal, we can directly embed the Boolean value using curly braces "{}" and prefix it with the "$" symbol. The Boolean value is automatically converted to its string representation, which is "True" for a true Boolean value and "False" for a false Boolean value.

Method 5: Using Boolean.ToString() Method

In C#, the Boolean type has its own ToString() method that allows you to directly convert a Boolean value to a string. 

bool boolValue = true;
string stringValue = boolValue.ToString();
Console.WriteLine("Boolean to String using Boolean.ToString(): " + stringValue);

Output:

Boolean to String using Boolean.ToString(): True

In this method, we call the ToString() method directly on the Boolean value, which is a built-in method of the Boolean type. The ToString() method of the Boolean type returns "True" if the Boolean value is true and "False" if the Boolean value is false.

Comments (0)

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