是否有一个原因的NullReferenceException不给变量的名称? [英] Is there a reason why NullReferenceException does not give the name of the variable?
问题描述
在 ArgumentNullException
有一个 PARAMNAME
属性,以指示哪个参数传递为空。
The ArgumentNullException
has a ParamName
property to indicate which argument was passed as null.
为什么在的NullReferenceException
再有类似的性质?难道一直技术上是可行的在.NET来实现?
Why does the NullReferenceException
not have a similar property? Would it have been technically possible to implement within .Net?
推荐答案
A 的NullReferenceException
由CLR抛出时它会尝试浏览一个空引用。这不一定与变量相关联,并且特别在CLR确实不在乎是从哪里来的 - 这是在堆栈上仅有一个值
A NullReferenceException
is thrown by the CLR when it tries to navigate a null reference. This isn't necessarily associated with a variable, and in particular the CLR really doesn't care where it came from - it's just a value on the stack.
相比之下,与 ArgumentNullException
是明确的通过code抛出,如:
Compare that with ArgumentNullException
which is explicitly thrown via code such as:
if (foo == null)
{
throw new ArgumentNullException("foo");
}
没有什么神奇这里 - 你甚至可以给错了名字,如果你想。因此,他们已经非常不同的情况。
There's no magic here - and you can even give the wrong name if you want. So they're really very different situations.
这篇关于是否有一个原因的NullReferenceException不给变量的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!