[英]string = string + int: What's behind the scenes?
In C# you can implicitly concatenate a string and let's say, an integer: 在C#中,你可以隐式地连接一个字符串,让我们说一个整数:
string sth = "something" + 0;
My questions are: 我的问题是:
Why, by assuming the fact that you can implicitly concatenate a string and an int, C# disallows initializing strings like this: 为什么,假设您可以隐式地连接字符串和int,C#不允许初始化字符串,如下所示:
string sth = 0; // Error: Cannot convert source type 'int' to target type 'string'
How C# casts 0 as string. C#如何将0转换为字符串。 Is it
0.ToString()
or (string)0
or something else? 是
0.ToString()
或(string)0
还是其他什么?
It compiles to a call to String.Concat(object, object)
, like this: 它编译为对
String.Concat(object, object)
的调用,如下所示:
string sth = String.Concat("something", 0);
(Note that this particular line will actually be optimized away by the compiler) (注意,这个特定的行实际上会被编译器优化掉)
This method is defined as follows: (Taken from the .Net Reference Source) 此方法定义如下:(取自.Net参考源)
public static String Concat(Object arg0, Object arg1) {
if (arg0==null) {
arg0 = String.Empty;
}
if (arg1==null) {
arg1 = String.Empty;
}
return Concat(arg0.ToString(), arg1.ToString());
}
(This calls String.Concat(string, string)
) (这调用
String.Concat(string, string)
)
To discover this, you can use ildasm
, or Reflector (in IL or in C# with no optimizations) to see what the +
line compiles to. 要发现这一点,您可以使用
ildasm
或Reflector(在IL或C#中没有优化)来查看+
行编译的内容。
This is specified in section 7.8.4 of the C# 4 spec: 这在C#4规范的第7.8.4节中规定:
For an operation of the form
x + y
, binary operator overload resolution (§7.3.4) is applied to select a specific operator implementation.对于
x + y
形式的操作,应用二元运算符重载决策(第7.3.4节)来选择特定的运算符实现。 The operands are converted to the parameter types of the selected operator, and the type of the result is the return type of the operator.操作数将转换为所选运算符的参数类型,结果的类型是运算符的返回类型。
The predefined addition operators are listed below.
下面列出了预定义的加法运算符。 For numeric and enumeration types, the predefined addition operators compute the sum of the two operands.
对于数字和枚举类型,预定义的加法运算符计算两个操作数的总和。 When one or both operands are of type string, the predefined addition operators concatenate the string representation of the operands.
当一个或两个操作数是字符串类型时,预定义的加法运算符会连接操作数的字符串表示形式。
The last sentence is the most relevant one to this situation. 最后一句与这种情况最相关。
Then later: 然后呢:
String concatenation
字符串连接
string operator +(string x, string y); string operator +(string x, object y); string operator +(object x, string y);
These overloads of the binary + operator perform string concatenation.
二进制+运算符的这些重载执行字符串连接。 If an operand of string concatenation is null, an empty string is substituted.
如果字符串连接的操作数为null,则替换空字符串。 Otherwise, any non-string argument is converted to its string representation by invoking the virtual
ToString
method inherited from type object.否则,通过调用从类型对象继承的虚拟
ToString
方法,将任何非字符串参数转换为其字符串表示形式。 IfToString
returns null, an empty string is substituted.如果
ToString
返回null,则替换空字符串。
That specifies how the integer is converted into a string. 这指定了整数如何转换为字符串。
And the result: 结果如下:
The result of the string concatenation operator is a string that consists of the characters of the left operand followed by the characters of the right operand.
字符串连接运算符的结果是一个字符串,其中包含左操作数的字符,后跟右操作数的字符。 The string concatenation operator never returns a null value.
字符串连接运算符永远不会返回空值。
The actual means of performing concatenation is implementation-specific, but as noted in other answers, the MS implementation uses string.Concat
. 执行连接的实际方法是特定于实现的,但是如其他答案中所述,MS实现使用
string.Concat
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.