"object.ToString()" 如何在字符串连接语句中求值?

How "object.ToString()" evaluate in string concatenate statement?

我的问题是 "object.ToString()" 方法如何在字符串连接语句中求值?

我的意思是如果写这样的代码-:

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);
}

它产生 -

Result = Employee Name Ni3 

所以编译器会像这样转换语句 -:

string result= String.Concat("Result = ",emp.ToString());

或者还有别的原因。

不完全是,您正在使用使用两个对象参数的 overload

并且 this 方法在 null 检查后对这些对象调用 .ToString()。见备注部分:

The method concatenates arg0 and arg1by calling the parameterless ToString method of arg0 and arg1; it does not add any delimiters.
String.Empty is used in place of any null argument.
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[]").