How to convert bool true or false to the string "True" or "False"

Is there an easy way for me to convert bool true or false to the string "True" or "False". I know that I can do this with some, if the logic, but I wonder if there is something even simpler.

+6
source share
4 answers

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

 bool b = true; Console.WriteLine(b.ToString()); 
+13
source

Call ToString ()

 System.Console.WriteLine(false.ToString()); System.Console.WriteLine(true.ToString()); 
+5
source

If you mean true and false , you can use Convert.ToString

 Convert.ToString(true) // "True" 

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

+1
source

To simply print β€œTrue” / β€œFalse,” readonly's built-in static fields in Boolean type:

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

Not that the bools value could change in the future or directly answer the OP question, but simply by adding some related information.

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

+1
source

Source: https://habr.com/ru/post/891064/


All Articles