[英]System.String does not overload operator += But String Concatenation works, How?
The System.String has only two Operator overloaded System.String只有两个运算符重载
public static bool operator ==(string a, string b)
{
return string.Equals(a, b);
}
public static bool operator !=(string a, string b)
{
return !string.Equals(a, b);
}
But when using += for String concat, Example : 但是当使用+ = for String concat时,示例:
private static void Main()
{
String str = "Hello ";
str += "World";
Console.WriteLine(str);
}
it works just fine, 它工作得很好,
So, how come if System.String doesn't overload the operator += it Concats the string? 那么,为什么System.String不会重载运算符+ = it Concats字符串?
First, the operator +=
can't be overloaded. 首先,运算符
+=
不能重载。 If you have the expression A += B
, it's compiled as if you wrote:* 如果你有表达式
A += B
,那就像你写的那样编译:*
A = A + B
Okay, that's why string
doesn't overload operator +=
(because it can't be overloaded). 好的,这就是为什么
string
不会重载operator +=
(因为它不能重载)。 So, why doesn't it overload operator +
either? 那么,为什么不重载
operator +
呢? It's just one more difference between the CLR and C#. 这只是CLR和C#之间的又一个区别。 The C# compiler knows that types like
string
and int
are special, and it generates special code for their operators (calling string.Concat()
for string
, or the add
instruction for int
). C#编译器知道像
string
和int
这样的类型是特殊的,它为它们的运算符生成特殊代码string.Concat()
为string
调用string.Concat()
,或者为int
add
指令)。
Why are these operators treated in a special way? 为什么这些运营商以特殊方式对待? Because you want them treated in a special way.
因为你希望他们以特殊的方式对待他们。 I think this is most clear for
int
: 我认为对于
int
来说这是最清楚的:
int
addition to be compiled as a method call, that would add a lot of overhead. int
添加编译为方法调用,这会增加很多开销。 Because of that, special instruction for int
addition is used. int
添加指令。 checked
and unchecked
operators. checked
和unchecked
运算符。 How should the compiler deal with that if it had only operator +
? operator +
,编译器应如何处理? (What it actually does is to use the instruction add
for unchecked overflows and add.ovf
for checked overflows.) add
对已检查的溢出使用add.ovf
。) And you want to treat string
addition in a special way too, for performance reasons. 并且出于性能原因,您还希望以特殊方式处理
string
添加。 For example, if you have string
s a
, b
and c
and write a + b + c
and then you compiled that as two calls to operator +
, you would need to allocate a temporary string
for the result of a + b
, which is inefficient. 例如,如果你有
string
s a
, b
和c
并写a + b + c
然后你编译为两个调用operator +
,你需要为a + b
的结果分配一个临时string
,这是效率低下。 Instead, the compiler generates that code as string.Concat(a, b, c)
, which can directly allocate only one string of the required length. 相反,编译器将该代码生成为
string.Concat(a, b, c)
,它只能直接分配一个所需长度的字符串。
* This is not exactly right, for details, see Eric Lippert's article Compound Assignment, Part One and Compound assignment in the C# specification. *这不完全正确,有关详细信息,请参阅Eric Lippert的文章化合物分配,第一部分和C#规范中的化合物分配 。 Also note the missing semicolons,
A += B
really is an expression, for example, you can write X += Y += Z;
还要注意缺少的分号,
A += B
确实是一个表达式,例如,你可以写X += Y += Z;
. 。
There is no overload of +=
operator, its a short-hand for var = var + newValue
. 没有
+=
运算符的重载,它是var = var + newValue
的简写 。 Same is the case with strings. 字符串的情况也是如此。
+= Operator (C# Reference) + =运算符(C#参考)
The += operator cannot be overloaded directly, but user-defined types can overload the + operator
+ =运算符不能直接重载,但用户定义的类型可能会使+运算符重载
Consider the following example: 请考虑以下示例:
string str = "new string";
str += "new value";
This is equal to : 这相当于:
str = str + "new value";
which internally calls string.Concat
at compile time. 在编译时内部调用
string.Concat
。
+=
is not explicitly implemented but it works because compiler do it's magic +=
没有明确实现,但它起作用,因为编译器做它的魔力
str += "World";
str = str + "World";
str = str.Concat("World");
As the guys above said, and according to .NET, string += otherString
is equivalent to 正如上面提到的那些人,根据.NET,
string += otherString
相当于
string = string + otherString
This .NET link mentions the concatenation operator, and this .NET link talks about the relationship between the two operations. 这个.NET链接提到了连接运算符, 这个.NET链接讨论了这两个操作之间的关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.