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

Jef*_*Cyr 7 .net c#

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

Console.WriteLine("ToString() -> " + o.ToString() ); //<--- Prints 'ToString() -> '
Console.WriteLine("GetType() -> " + o.GetType()); //<--- NullReferenceException
Run Code Online (Sandbox Code Playgroud)

输出:

ToString() -> 

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

什么是类型???以及为什么o.ToString()返回string.Emptyo.GetType()抛出NullReferenceException

注意:GetType()未在???类型中重新定义.

Dyn*_*ard 7

任何Nullable<T>.

检查Gravell的例子到C#中的奇怪角点案例

  • 一个更有趣的例子是同一答案的"MyFunnyType". (2认同)