C# 为什么 Boolean.ToString 输出“真”而不是“真”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/491334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:26:03  来源:igfitidea点击:

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

c#.netboolean

提问by Chris S

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).

它是“真实”而不是“真实”是否有正当理由?当将 XML 编写为 XML 的 boolean 类型是小写时,它会中断,并且与 C# 的 true/false 不兼容(虽然不确定 CLS)。

Update

更新

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

这是我在 C# 中解决它的非常hacky 的方法(用于 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.

当然,这向堆栈中添加了 1 个方法,但删除了所有地方的 ToLowers()。

采纳答案by Vojislav Stojkovic

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:

首先,这是 MSDN 中关于Boolean.ToString()方法的说明:

Return Value

Type: System.String

TrueStringif the value of this instance is true, or FalseStringif 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.

返回值

类型:System.String

TrueString如果此实例的值是true,或者FalseString如果此实例的值是假的。

评论

此方法返回常量“True”或“False”。请注意,XML 区分大小写,并且 XML 规范将“true”和“false”识别为有效的布尔值集。如果要将 ToString() 方法返回的 String 对象写入 XML 文件,则应首先调用其 String.ToLower 方法将其转换为小写。

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.

有趣的事实 #1 来了:它根本不返回 TrueString 或 FalseString。它使用硬编码文字“True”和“False”。如果它使用这些字段对你没有任何好处,因为它们被标记为只读,所以没有改变它们。

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

另一种方法Boolean.ToString(IFormatProvider)甚至更有趣:

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.

评论

provider 参数是保留的。它不参与该方法的执行。这意味着 Boolean.ToString(IFormatProvider) 方法与大多数具有提供程序参数的方法不同,它不反映特定于区域性的设置。

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

解决办法是什么?取决于你到底想做什么。不管是什么,我敢打赌它需要破解 ;)

回答by Rune Grimstad

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".

它如何与 C# 不兼容?Boolean.Parse 和 Boolean.TryParse 不区分大小写,解析是通过将值与“True”和“False”的 Boolean.TrueString 或 Boolean.FalseString 进行比较来完成的。

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:

编辑:当查看反射器中的 Boolean.ToString 方法时,结果证明字符串是硬编码的,因此 ToString 方法如下:

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

回答by John

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.

是我用于 xml 输出的。

回答by bruno conde

For Xml you can use XmlConvert.ToStringmethod.

对于 Xml,您可以使用XmlConvert.ToString方法。

回答by stusmith

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

...因为 .NET 环境旨在支持多种语言。

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'.

System.Boolean(在 mscorlib.dll 中)旨在供语言内部使用以支持布尔数据类型。C# 的关键字全部使用小写字母,因此使用“bool”、“true”和“false”。

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

然而,VB.NET 使用标准大小写:因此是“Boolean”、“True”和“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.

由于语言必须协同工作,因此您不能让 true.ToString() (C#) 给出与 True.ToString() (VB.NET) 不同的结果。CLR 设计者为 ToString() 结果选择了标准的 CLR 大小写符号。

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

布尔值 true 的字符串表示被定义为 Boolean.TrueString。

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

(System.String 也有类似的情况:C# 将其表示为“字符串”类型)。

回答by cjk

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

这可能是从旧的 VB NOT .Net 时代开始的,当时 bool.ToString 产生 True 或 False。

回答by Loudenvier

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");

回答by Kody

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

原因true是“True”是因为 Microsoft 与 XML 标准的紧密联系。

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-readableand machine-readable."

来自维基百科:“可扩展标记语言 (XML) 是一种标记语言,它定义了一组规则,用于以人类可读和机器可读的格式对文档进行编码。”

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").

人类可读是主观的,但在 XML 眼中,首选使用单词“One”代替数字“1”。您会注意到使用枚举会发生这种情况,因为单词被序列化而不是其值(“FirstOption”而不是“0”或“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.

同样,文本通常遵循CamelCasing。因此,XML 更喜欢“字符串”而不是“字符串”。这就是默认情况下 Boolean.TrueString 为“True”而 Boolean.FalseString 为“False”的原因。