Coder Perfect

Why does Boolean.ToString return “True” rather than “true”?

Problem

true.ToString() 
false.toString();

Output:
True
False

Is there a good reason why it’s “True” instead of “true”? It fails while writing XML because the boolean type in XML is lower case, and it also clashes with Ctrue/false. #’s (not sure about CLS though).

Update

In C#, here’s how I got around it in a clumsy way (for use with XML)

internal static string ToXmlString(this bool b)
{
    return b.ToString().ToLower();
}

Of course, this adds another function to the stack, but it also removes ToLowers() from all places.

Asked by Chris S

Solution #1

That is a question that only Microsoft employees can truly answer. However, I’d like to share some interesting facts with you 😉

First, here’s what MSDN says about the Boolean. ToString() is a method that converts a string into a string.

The first fun fact is that it does not return TrueString or FalseString. The literals “True” and “False” are hardcoded. It wouldn’t help you if it utilised the fields because they’re marked as readonly and can’t be changed.

Boolean is an alternative method. Even hilarious is ToString(IFormatProvider):

So, what’s the answer? It all depends on what you’re attempting to accomplish. Whatever it is, I’m sure it’ll necessitate a hack 😉

Answered by Vojislav Stojkovic

Solution #2

…since the.NET framework is built to accommodate a wide range of languages.

System. Boolean (in mscorlib.dll) is a class that languages can use internally to support the boolean datatype. The keywords in C# are all lowercase, therefore ‘bool’, ‘true’, and ‘false’ are all lowercase.

VB.NET, on the other hand, employs conventional casing, as evidenced by the terms ‘Boolean,’ ‘True,’ and ‘False.’

True couldn’t exist since the languages had to function together. ToString() (C#) returns a result that differs from True. ToString() is a function that converts a string to (VB.NET). For the ToString() output, the CLR designers chose the usual CLR case notation.

The boolean true’s string representation is specified as Boolean. TrueString.

(System is in the same boat.) String: The’string’ type is used in C#.

Answered by stusmith

Solution #3

You can use the XmlConvert.ToString function for Xml.

Answered by bruno conde

Solution #4

Converting that to all lower case is a straightforward code.

However, converting “true” back to “True” is not so simple.

true.ToString().ToLower() 

For xml output, this is what I use.

Answered by John

Solution #5

What makes it incompatible with C#? Boolean. Parse and Boolean are two terms that are often used interchangeably. TryParse is case-insensitive, and it parses data by comparing it to a Boolean value. TrueString or Boolean are both valid options. TrueString and FalseString are FalseString.

EDIT: Reflector reveals that the strings in the Boolean.ToString function are hard coded, therefore the ToString method is as follows:

public override string ToString()
{
    if (!this)
    {
        return "False";
    }
    return "True";
}

Answered by Rune Grimstad

Post is based on https://stackoverflow.com/questions/491334/why-does-boolean-tostring-output-true-and-not-true