C# 如果转换为 null,为什么 Convert.ToString(null) 返回不同的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10355736/
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
Why does Convert.ToString(null) return a different value if you cast null?
提问by John MacIntyre
Convert.ToString(null)
returns
返回
null
As I expected.
正如我所料。
But
但
Convert.ToString(null as object)
returns
返回
""
Why are these different?
为什么这些不同?
采纳答案by JaredPar
There are 2 overloads of ToStringthat come into play here
有2个重载ToString,这里有了用武之地
Convert.ToString(object o);
Convert.ToString(string s);
The C# compiler essentially tries to pick the most specific overload which will work with the input. A nullvalue is convertible to any reference type. In this case stringis more specific than objectand hence it will be picked as the winner.
C# 编译器本质上会尝试选择最适合输入的重载。甲null值转换为任何引用类型。在这种情况下string比更具体object,因此它将被选为获胜者。
In the null as objectyou've solidified the type of the expression as object. This means it's no longer compatible with the stringoverload and the compiler picks the objectoverload as it's the only compatible one remaining.
在null as object您已将表达式的类型固化为object. 这意味着它不再与string重载兼容,编译器会选择object重载,因为它是唯一剩余的兼容重载。
The really hairy details of how this tie breaking works is covered in section 7.4.3 of the C# language spec.
C# 语言规范的第 7.4.3 节介绍了这种打破平局如何工作的真正毛骨悚然的细节。
回答by Mark Brackett
Following on from JaredPar's excellent overload resolutionanswer - the question remains "why does Convert.ToString(string)return null, but Convert.ToString(object)return string.Empty"?
继JaredPar 出色的重载解析答案之后 - 问题仍然是“为什么Convert.ToString(string)返回 null,但Convert.ToString(object)返回string.Empty”?
And the answer to that is...because the docssay so:
Convert.ToString(string) returns "the specified string instance; no actual conversion is performed."
Convert.ToString(object) returns "the string representation of value, or String.Empty if value is null."
Convert.ToString(string) 返回“指定的字符串实例;不执行实际转换。”
Convert.ToString(object) 返回“值的字符串表示形式,如果值为 null,则返回 String.Empty”。
EDIT: As to whether this is a "bug in the spec", "very bad API design", "why was it specified like this", etc. - I'll take a shot at some rationale for why I don't see it as big deal.
编辑:至于这是否是“规范中的错误”、“非常糟糕的 API 设计”、“为什么这样指定”等 - 我将尝试解释为什么我看不到的一些理由大不了。
System.Converthas methods for converting every base type to itself. This is strange - since no conversion is needed or possible, so the methods end up just returning the parameter.Convert.ToString(string)behaves the same. I presume these are here for code generation scenarios.Convert.ToString(object)has 3 choices when passednull. Throw, return null, or return string.Empty. Throwing would be bad - doubly so with the assumption these are used for generated code. Returning null requires your caller do a null check - again, not a great choice in generated code. Returning string.Empty seems a reasonable choice. The rest ofSystem.Convertdeals with value types - which have a default value.- It's debatable whether returning null is more "correct", but string.Empty is definitely more usable. Changing
Convert.ToString(string)means breaking the "no actual conversion" rule. SinceSystem.Convertis a static utility class, each method can be logically treated as its own. There's very few real world scenarios where this behavior should be "surprising", so let usability win over (possible) correctness.
System.Convert具有将每个基本类型转换为自身的方法。这很奇怪——因为不需要或不可能进行转换,所以这些方法最终只返回参数。Convert.ToString(string)行为相同。我认为这些是用于代码生成场景的。Convert.ToString(object)通过时有 3 个选择null。抛出、返回 null 或返回 string.Empty。投掷会很糟糕 - 假设这些用于生成的代码,则更是如此。返回 null 需要您的调用者进行 null 检查 - 同样,在生成的代码中这不是一个很好的选择。返回 string.Empty 似乎是一个合理的选择。其余的System.Convert处理值类型 - 具有默认值。- 返回 null 是否更“正确”是有争议的,但 string.Empty 肯定更有用。改变
Convert.ToString(string)意味着打破“无实际转换”规则。由于System.Convert是一个静态实用程序类,每个方法都可以在逻辑上被视为自己的方法。这种行为应该“令人惊讶”的现实世界场景很少,所以让可用性战胜(可能的)正确性。

