Why does Boolean.ToString output "True" and not "true"

C#.NetBoolean

C# Problem Overview


true.ToString() 
false.toString();

Output:
True
False

Is there a valid reason for it being "True" and not "true"? It breaks when writing XML as XML's boolean type is lower case, and also isn't compatible with C#'s true/false (not sure about CLS though).

Update

Here is my very hacky way of getting around it in C# (for use with XML)

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

Of course that adds 1 more method to the stack, but removes ToLowers() everywhere.

C# Solutions


Solution 1 - C#

Only people from Microsoft can really answer that question. However, I'd like to offer some fun facts about it ;)

First, this is what it says in MSDN about the Boolean.ToString() method:

> Return Value > > Type: System.String > > TrueString if the value of this > instance is true, or FalseString if > the value of this instance is false. > > Remarks > > This method returns the > constants "True" or "False". Note that > XML is case-sensitive, and that the > XML specification recognizes "true" > and "false" as the valid set of > Boolean values. If the String object > returned by the ToString() method > is to be written to an XML file, its > String.ToLower method should be > called first to convert it to > lowercase.

Here comes the fun fact #1: it doesn't return TrueString or FalseString at all. It uses hardcoded literals "True" and "False". Wouldn't do you any good if it used the fields, because they're marked as readonly, so there's no changing them.

The alternative method, Boolean.ToString(IFormatProvider) is even funnier:

> Remarks > > The provider parameter is reserved. It does not participate in the execution of this method. This means that the Boolean.ToString(IFormatProvider) method, unlike most methods with a provider parameter, does not reflect culture-specific settings.

What's the solution? Depends on what exactly you're trying to do. Whatever it is, I bet it will require a hack ;)

Solution 2 - C#

...because the .NET environment is designed to support many languages.

System.Boolean (in mscorlib.dll) is designed to be used internally by languages to support a boolean datatype. C# uses all lowercase for its keywords, hence 'bool', 'true', and 'false'.

VB.NET however uses standard casing: hence 'Boolean', 'True', and 'False'.

Since the languages have to work together, you couldn't have true.ToString() (C#) giving a different result to True.ToString() (VB.NET). The CLR designers picked the standard CLR casing notation for the ToString() result.

The string representation of the boolean true is defined to be Boolean.TrueString.

(There's a similar case with System.String: C# presents it as the 'string' type).

Solution 3 - C#

For Xml you can use XmlConvert.ToString method.

Solution 4 - C#

It's simple code to convert that to all lower case.

Not so simple to convert "true" back to "True", however.

true.ToString().ToLower() 

is what I use for xml output.

Solution 5 - C#

How is it not compatible with C#? Boolean.Parse and Boolean.TryParse is case insensitive and the parsing is done by comparing the value to Boolean.TrueString or Boolean.FalseString which are "True" and "False".

EDIT: When looking at the Boolean.ToString method in reflector it turns out that the strings are hard coded so the ToString method is as follows:

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

Solution 6 - C#

I know the reason why it is the way it is has already been addressed, but when it comes to "custom" boolean formatting, I've got two extension methods that I can't live without anymore :-)

public static class BoolExtensions
{
    public static string ToString(this bool? v, string trueString, string falseString, string nullString="Undefined") {
        return v == null ? nullString : v.Value ? trueString : falseString;
    }
    public static string ToString(this bool v, string trueString, string falseString) {
        return ToString(v, trueString, falseString, null);
    }
}

Usage is trivial. The following converts various bool values to their Portuguese representations:

string verdadeiro = true.ToString("verdadeiro", "falso");
string falso = false.ToString("verdadeiro", "falso");
bool? v = null;
string nulo = v.ToString("verdadeiro", "falso", "nulo");

Solution 7 - C#

The reason true is "True" is because of Microsoft's strong bond with XML standards.

From Wikipedia: "Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format which is both human-readable and machine-readable."

Human-readable is subjective, but in the eyes of XML, using the word "One" in place of a number "1" is preferred. You'll notice this happens using enums, as the word gets serialized instead of its value ("FirstOption" instead of "0" or "1").

Likewise, text commonly follows CamelCasing. Therefore, instead of "string", XML prefers "String". This is why Boolean.TrueString is "True" and Boolean.FalseString is "False" by default.

Solution 8 - C#

This probably harks from the old VB NOT .Net days when bool.ToString produced True or False.

Solution 9 - C#

    public bool CheckBool(string res)
    {
        if (res == null)
        {
            return false;
        }

        if (res.ToLower() == "true")
        {
            return true;
        }
        if (res.ToLower() == "false")
        {
            return false;
        }
        if (res == "0")
        {
            return false;
        }
        if (res == "1")
        {
            return true;
        }
        else
        {
            return bool.Parse(res);
        }
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris SView Question on Stackoverflow
Solution 1 - C#Vojislav StojkovicView Answer on Stackoverflow
Solution 2 - C#stusmithView Answer on Stackoverflow
Solution 3 - C#bruno condeView Answer on Stackoverflow
Solution 4 - C#JohnView Answer on Stackoverflow
Solution 5 - C#Rune GrimstadView Answer on Stackoverflow
Solution 6 - C#LoudenvierView Answer on Stackoverflow
Solution 7 - C#KodyView Answer on Stackoverflow
Solution 8 - C#cjkView Answer on Stackoverflow
Solution 9 - C#KemboiView Answer on Stackoverflow