我怎样才能得到这个code样品中一个NullReferenceException? [英] How can I get a NullReferenceException in this code sample?
本文介绍了我怎样才能得到这个code样品中一个NullReferenceException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
??? O =新???();
Console.WriteLine(的ToString() - >中+ o.ToString()); //< ---打印'的ToString() - GT;
Console.WriteLine(的GetType() - >中+ o.GetType()); //< ---的NullReferenceException
输出:
的ToString() - GT;
未处理的异常:System.NullReferenceException:对象引用未设置
为一个对象的一个实例。
在System.Object.GetType()
在Program.Main(字串[] args)
问题
什么是类型 ???
?为什么 o.ToString()
返回的String.Empty
和 o.GetType()
抛出一个的NullReferenceException
?
注意:
的GetType()
在不重新定义了???
键入
解决方案
任何可空< T>
检查<一href="http://stackoverflow.com/questions/194484/whats-the-strangest-corner-case-youve-seen-in-c-or-net/194671#194671">Gravell's例如奇怪的角落情况下,在C#
??? 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()
throws a NullReferenceException
?
Note:
GetType()
is not redefined in the???
type.
解决方案
Any Nullable<T>
.
Check Gravell's example to strange corner cases in C#
这篇关于我怎样才能得到这个code样品中一个NullReferenceException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文