What is the easiest way to convert from bool to string in C #?

I am tempted to use if ... else ... but I wonder if there is an easier way? I need to display true or false result in a message box.

+4
source share
10 answers

bool.ToString already doing what you want.

This method returns the constants True or False.

However, in practice it is often not necessary to explicitly call ToString directly from your code. If you are already writing a string, the easiest way is to use concatenation:

 string message = "The result is " + b; 

This string.Concat to call string.Concat , and it calls the ToString method for you.

In some situations, it is useful to use String.Format , and the ToString method is called again:

 string message = string.Format("The result is {0}. Try again?", b); 
+14
source
 bool b = true; Console.WriteLine(b.ToString()); 
+9
source

I'm just going to quit:

 val ? "true" : "false" 

into the mix, as often lower-case results are needed (many machine-readable formats, such as many XML formats, use lowercase "true" and "false" for boolean values) and higher, as well as IMO cleaner than val.ToString().ToLowerInvariant() .

Of course, an extension to val ? "yes" : "no" val ? "yes" : "no" , etc. trivially. Being localizable is another matter, and not always trivial (quite a lot of cases would have had completely different sentences if they were well translated, so the logic messageStart + (val ? "yes" : "no") + messageEnd does not always work well.

+6
source
 bool v = true; string s = v.ToString(); 
+2
source

What is wrong with .ToString() , which is available in every object?

 bool myBool = false; string myString = myBool.ToString(); 
+2
source

This is single line. All C # types come from Object and therefore inherit methods from this class.

I will point you here: http://msdn.microsoft.com/en-us/library/system.object.aspx and let it find it.

+1
source
  bool myBool = true; MessageBox.show(myBool.toString()); 
+1
source

It is always recommended to use static functions in the Convert class. In your case

bool boolValue = true; System.Convert.ToString (boolValue);

+1
source

If you want to make it fully localizable, you can add a resource file called UiStrings to the project and add an entry for each of the Boolean values. Visual Studio will create a wrapper (using PublicResXFileCodeGenerator or ResXFileCodeGenerator) for the resource manager, which you can access using static properties.

Then you can use it as follows:

 var booleanValue = true; var booleanText = booleanValue ? UiStrings.TrueValueText : UiStrings.FalseValueText; var messageText = UiString.ResultMessageText; var messageString = String.Format("{0}:{1}", messageText, booleanText); 

The detailed code is intentional so that you can identify the different parts.

0
source

I know that you did not ask about it, but in order to add your own narcissistic voice to the mix, here is how you get int from bool (0, 1)

 using System; class Program { static void Main() { // Example bool is true bool t = true; // A // Convert bool to int int i = t ? 1 : 0; Console.WriteLine(i); // 1 // Example bool is false bool f = false; // B // Convert bool to int int y = Convert.ToInt32(f); Console.WriteLine(y); // 0 } } 

Conclusion:

1

0

-1
source

All Articles