<分区>
Possible Duplicate:
C#: Passing null to overloaded method - which method is called?
这是一个测试用例
object a = null;
var b = Convert.ToString (null);
var c = Convert.ToString (a);
string d = Convert.ToString (null); // CLR chooses Convert.ToString(string value)
string e = Convert.ToString (a); // CLR chooses Convert.ToString(object value)
问题是为什么 CLR 决定在第一种情况下将 null 解释为字符串? 看来这个问题已经有人回答了here
这是另一个类似的案例。这些 if 都不会被触发
object x = null;
if (x is object)
{
Console.Write ("x is object");
}
if (x is string)
{
Console.Write ("x is string");
}
if (null is object)
{
Console.Write ("null is object");
}
if (null is string)
{
Console.Write ("null is string");
}