如何在此代码示例中获取NullReferenceException?

时间:2010-02-12 16:01:41

标签: c# .net

??? o = new ???();

Console.WriteLine("ToString() -> " + o.ToString() ); //<--- Prints 'ToString() -> '
Console.WriteLine("GetType() -> " + o.GetType()); //<--- NullReferenceException

输出:

ToString() -> 

Unhandled Exception: System.NullReferenceException: Object reference not set 
to an instance of an object.
at System.Object.GetType()
at Program.Main(String[] args)

问题

???类型是什么?为什么o.ToString()返回string.Emptyo.GetType()会引发NullReferenceException

  

注意GetType()类型未重新定义???

1 个答案:

答案 0 :(得分:7)