问题描述
true.ToString()
false.toString();
Output:
True
False
有没有为它是真实的,而不是真实的正当理由?编写XML作为XML的布尔类型,当它打破的是小写的,并且也不是用C#的真/假(不知道CLS虽然)。
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).
更新
下面是我非常哈克周围越来越在C#中(用于XML使用)的方式
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();
}
当然,增加了1更多的方法来堆栈,但删除ToLowers()无处不在。
Of course that adds 1 more method to the stack, but removes ToLowers() everywhere.
推荐答案
从微软只有人能真正回答这个问题。不过,我想提供关于它的一些有趣的事实;)
Only people from Microsoft can really answer that question. However, I'd like to offer some fun facts about it ;)
首先,这就是它说的MSDN关于 Boolean.ToString()方法:
First, this is what it says in MSDN about the Boolean.ToString() method:
返回值
类型:System
TrueString 的,如果这个值 实例是真实的,或者的 FalseString 的若 这个实例的值是假的。
TrueString if the value of this instance is true, or FalseString if the value of this instance is false.
备注
该方法返回 常量真或假。注意 XML是大小写敏感的,并且该 XML规范承认真 和假作为有效组 布尔值。如果String对象 由ToString()方法返回 是要被写入到一个XML文件,其 String.ToLower方法应该 称为第一将其转换为 小写的。
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.
下面到了有趣的事实#1:它不会返回TrueString或FalseString的。它采用硬codeD文字真与假。不会做你任何好处,如果它使用的领域,因为他们作为标记为只读,因此没有改变他们。
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.
另一种方法, Boolean.ToString(的IFormatProvider)是更有趣:
The alternative method, Boolean.ToString(IFormatProvider) is even funnier:
备注
该提供参数保留。它不参与该方法的执行。这意味着,Boolean.ToString(的IFormatProvider)方法,不像大多数方法与提供者参数,不反映培养特定的设置。
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 ;)
这篇关于为什么Boolean.ToString输出"真"和不与QUOT;真"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!