c# – 如何在此代码示例中获得NullReferenceException?

前端之家收集整理的这篇文章主要介绍了c# – 如何在此代码示例中获得NullReferenceException?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
??? 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.Empty和o.GetType()会抛出NullReferenceException?

Note: GetType() is not redefined in the ??? type.

解决方法

原文链接:https://www.f2er.com/csharp/99307.html

猜你在找的C#相关文章