开发者

How can I get a NullReferenceException in this code sample?

开发者 https://www.devze.com 2022-12-20 12:52 出处:网络
??? o = new ???(); Console.WriteLine(\"ToString() -> \" + o.ToString() ); //<--- Prints \'ToString() -> \'
??? 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

What is the type ??? and why does o.ToString() return string.Empty and o.GetType() thr开发者_运维百科ows a NullReferenceException?

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


Any Nullable<T>.

Check Gravell's example to strange corner cases in C#

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号