C# 为什么向字符串添加 null 是合法的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/637308/
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 is adding null to a string legal?
提问by David Hodgson
The MSDN article on String Basicsshows this:
关于字符串基础的 MSDN 文章显示了这一点:
string str = "hello";
string nullStr = null;
string emptyStr = "";
string tempStr = str + nullStr; // tempStr = "hello"
bool b = (emptyStr == nullStr);// b = false;
string newStr = emptyStr + nullStr; // creates a new empty string
int len = nullStr.Length; // throws NullReferenceException
Why doesn't concatenating with null throw a null reference exception? Is it to make a programmer's life easier, such that they don't have to check for null before concatenation?
为什么不与 null 连接会引发空引用异常?是为了让程序员的生活更轻松,这样他们就不必在连接之前检查 null 了吗?
采纳答案by CMS
From MSDN:
从MSDN:
In string concatenation operations, the C# compiler treats a null string the same as an empty string, but it does not convert the value of the original null string.
在字符串连接操作中,C# 编译器将空字符串视为空字符串,但不会转换原始空字符串的值。
More information on the + binary operator:
有关+ 二元运算符的更多信息:
The binary + operator performs string concatenation when one or both operands are of type string.
If an operand of string concatenation is null, an empty string is substituted. Otherwise, any non-string argument is converted to its string representation by invoking the virtual ToString method inherited from type object.
If ToString returns null, an empty string is substituted.
当一个或两个操作数都是字符串类型时,二元 + 运算符执行字符串连接。
如果字符串连接的操作数为空,则替换为空字符串。否则,通过调用从类型对象继承的虚拟 ToString 方法,将任何非字符串参数转换为其字符串表示形式。
如果 ToString 返回 null,则替换为空字符串。
回答by tarn
The reason it doesn't throw a null reference exception is because you're not actually trying to access any properties or methods on the null object. As CMS quotes, when concatenating string, nulls are replaced with empty strings.
它不抛出空引用异常的原因是您实际上并未尝试访问空对象上的任何属性或方法。作为 CMS 引号,当连接字符串时,空值被替换为空字符串。
回答by Dana
I guess the language (or standard library) designers decided this would be a common enough case that they'd do programmers a favour.
我猜语言(或标准库)设计者认为这将是一个足够常见的案例,他们会帮程序员一个忙。
(Neat! I always just assumed concating with null would through an exception!)
(好!我总是假设与 null 连接会通过异常!)
回答by Mark Cidade
Conceptually, strings are normally thought of as values as opposed to references to objects which have identity. One of the main reasons that they aren't struct
s with value semantics is because of the overhead that comes with copying-on-assignment. If strings werevalues they couldn't be nullable and so a null
is just treated by the "+" operator as if it were an empty string (i.e., as if default(string) == ""
just as default(int) == 0
).
从概念上讲,字符串通常被认为是值,而不是对具有标识的对象的引用。它们不struct
具有值语义的主要原因之一是因为赋值时复制带来的开销。如果字符串是值,则它们不能为空,因此 anull
只是被“+”运算符视为空字符串(即,default(string) == ""
就像default(int) == 0
)。
回答by Mark Cidade
I agree that conceptually strings are just values. However, consider the following code:
我同意概念上的字符串只是值。但是,请考虑以下代码:
int? i = null;
i += 1; // The result of this is that i == null
If the other value type operators used default() the way the string operators are converting null to "", your explanation would make sense.
如果其他值类型运算符使用 default() 字符串运算符将 null 转换为 "" 的方式,那么您的解释将有意义。
It's simplest to say that the string operators are a shortcut (special case) for convenience.
为方便起见,最简单的说法是字符串运算符是一种快捷方式(特殊情况)。