Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

C#/.NET - convert bool to string

0 contributions
14 points

In C#/.NET number string can be created in few ways.

1. ToString example

bool value = true;
string text = value.ToString();
Console.WriteLine(text); // True

Output:

True

2. Convert.ToString example

bool value = true;
string text = Convert.ToString(value);
Console.WriteLine(text); // True

Output:

True

3. string.Format example

bool value = true;
string text = string.Format("{0}", value);
Console.WriteLine(text); // True

Output:

True

4. String interpolation example

bool value = true;
string text = $"{value}";
Console.WriteLine(text); // True

Output:

True
Note: this feature available is in C# 6 and later versions 

5. Summing string and number example (empty string)

bool value = true;
string text = "" + value; 
// string text = value + "";
// string text = string.Empty + value; 
// string text = value + string.Empty;
Console.WriteLine(text); // True

Output:

True

6. StringBuilder example

bool value = true;
string text = new StringBuilder().Append(value).ToString();
Console.WriteLine(text); // True

Output:

True

7. TypeConverter example

TypeConverter converter = TypeDescriptor.GetConverter(typeof(bool));

bool value = true;
string text = (string)converter.ConvertTo(value, typeof(string));
Console.WriteLine(text); // True

Output:

True

8. Custom conversion example

bool value = true;
string text = value ? "True" : "False";
Console.WriteLine(text); // True

Output:

True
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more