Jul 8, 2008

3 ways to get true / false as string in C#

Who needs that much? Well, it's good to choose one and keep up with it.

The first, oldfashioned way is to write the bool as string:

string trueStr = "True";

But how would you know if the first letter should be uppercased?


There are two better ways to get the True string or False string:

string trueStr = true.ToString();
string trueStr = false.ToString()

And the last method is to use the bool type :

string trueStr = bool.TrueString;
string trueStr = bool.FalseString;

I think it is best to use the last one as it seems to me it was added exactly for such purposes.
>

No comments: