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

2023-12-19

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

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

Output:

ToString() -> 

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

Question

类型是什么???为什么o.ToString() return string.Empty and o.GetType()抛出一个NullReferenceException?

Note: GetType()没有重新定义??? type.


Any Nullable<T>.

Check Gravell 的 C# 中奇怪的极端情况示例 https://stackoverflow.com/questions/194484/whats-the-strangest-corner-case-youve-seen-in-c-or-net/194671#194671

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在此代码示例中获得 NullReferenceException? 的相关文章

随机推荐