简体   繁体   中英

How can I convert bool true or false to string “True” or “False”

Is there a simple way for me to convert bool true or false to string "True" or "False". I know I can do this with some if logic but I am wondering if there is something even simpler.

The Boolean structure has a ToString() method. So:

bool b = true;
Console.WriteLine(b.ToString());

Call ToString()

System.Console.WriteLine(false.ToString());
System.Console.WriteLine(true.ToString());

To simply print "True" / "False", there are built in static readonly fields on Boolean type:

string falseString = bool.FalseString;
string trueString = bool.TrueString;

Not that the value of bools might change in future or directly answers OP's question, but just adding some related info.

http://msdn.microsoft.com/en-us/library/system.boolean_fields(v=vs.100).aspx

If you mean the values true and false you can use Convert.ToString

Convert.ToString(true) // "True"

EDIT : mattn has the better answer, I was translating code from VB where the True keyword did not have a ToString() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM