There are 2 overloads of ToString
that come into play here
Convert.ToString(object o);
Convert.ToString(string s);
The C# compiler essentially tries to pick the most specific overload which will work with the input. A null
value is convertible to any reference type. In this case string
is more specific than object
and hence it will be picked as the winner.
In the null as object
you've solidified the type of the expression as object
. This means it's no longer compatible with the string
overload and the compiler picks the object
overload as it's the only compatible one remaining.
The really hairy details of how this tie breaking works is covered in section 7.4.3 of the C# language spec.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…