Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to convert from a bool to string in C#?

Tags:

c#

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

like image 774
Behrooz Karjoo Avatar asked Nov 27 '22 02:11

Behrooz Karjoo


1 Answers

The bool.ToString method already does what you want.

This method returns the constants "True" or "False".

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

string message = "The result is " + b;

This compiles to a call to string.Concat and this calls the ToString method for you.

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

string message = string.Format("The result is {0}. Try again?", b);
like image 198
Mark Byers Avatar answered Nov 29 '22 15:11

Mark Byers