简体   繁体   English

string = string + int:幕后是什么?

[英]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: 我的问题是:

  1. 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' 
  2. How C# casts 0 as string. C#如何将0转换为字符串。 Is it 0.ToString() or (string)0 or something else? 0.ToString()(string)0还是其他什么?

  3. How to find an answer of the previous question? 如何找到上一个问题的答案?

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方法,将任何非字符串参数转换为其字符串表示形式。 If ToString 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.

相关问题 清单支持的OS设置在幕后实际上做了什么? - What does a manifest's supportedOS setting actually do behind the scenes? 大型LINQ分组查询,幕后发生了什么 - Large LINQ Grouping query, what's happening behind the scenes Char to int隐式演员幕后故事 - Char to int implicit cast Behind the Scenes int32.Parse(String, IFormatProvider) 在 int32.Parse(String) 上的用例是什么? - What's the use case for int32.Parse(String, IFormatProvider) over int32.Parse(String)? MaskStore 在幕后做了什么? - What does MaskStore do behind the scenes? 有人可以解释幕后发生的事情吗? - Can someone explain what is happening behind the scenes? 在幕后,C#/ .NET中十进制值类型发生了什么? - Behind the scenes, what's happening with decimal value type in C#/.NET? Azure 表存储,帮我弄清楚批量操作的幕后发生了什么 - Azure table storage, help me clarify what's happening behind the scenes in batch operation 如果在使用块中进行了返回调用,那么幕后操作的顺序是什么? - If a call to return is made in a using block, what is the order of operation behind the scenes? LINQ to objects 'where' 子句在幕后做什么? - What is the LINQ to objects 'where' clause doing behind the scenes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM