[英]How “object.ToString()” evaluate in string concatenate statement?
My question is how "object.ToString()" method evaluate in string concatenate statement?. 我的问题是如何在字符串连接语句中评估“ object.ToString()”方法?
I mean if write code like-: 我的意思是,如果编写类似以下代码:
class Employee
{
public string Name{get; set;}
public override string ToString()
{
return string.Format("Employee Name {0}", Name);
}
}
static void Main()
{
Employee emp = new Employee() {Name = "Ni3"};
string result = "Result = " + emp;
Console.WriteLine(result);
}
it yields - 它产生-
Result = Employee Name Ni3
so compiler transforms the statement like this -: 因此,编译器将这样的语句转换为-:
string result= String.Concat("Result = ",emp.ToString());
or there is another reason. 或还有其他原因。
Not quite, you are using the overload that uses two object parameters. 不完全是,您正在使用使用两个对象参数的重载 。
And this method calls .ToString()
on those objects, after a null-check. 在进行空检查之后, 此方法在这些对象上调用.ToString()
。 See the remarks section: 请参阅备注部分:
The method concatenates arg0 and arg1by calling the parameterless ToString method of arg0 and arg1; 该方法通过调用arg0和arg1的无参数ToString方法来连接arg0和arg1。 it does not add any delimiters. 它不添加任何定界符。
String.Empty is used in place of any null argument. 使用String.Empty代替任何null参数。
If either of the arguments is an array reference, the method concatenates a string representing that array, instead of its members (for example, "System.String[]"). 如果两个参数中的任何一个都是数组引用,则该方法将代表该数组的字符串而不是其成员连接起来(例如,“ System.String []”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.