简体   繁体   English

为什么这会引发空引用异常?

[英]Why does this throw a null reference exception?

This will throw a null reference exception when InnerException is null. 当InnerException为null时,这将抛出一个空引用异常。

String s = " inner exception: " + e.InnerException == null ? "None" : e.InnerException.Message;

but this won't: 但这不会:

String s = " inner exception: " + (e.InnerException == null ? "None" : e.InnerException.Message);

Both of the above build fine. 以上两种都很好。 I can't figure out what the former is trying to do that would cause it to evaluate e.InnerException.Message . 我无法弄清楚前者试图做什么会导致它评估e.InnerException.Message Why aren't they equivalent? 为什么它们不相同?

This is because your first statement is evaluating " inner exception: " + e.InnerException == null to be true or false . 这是因为您的第一个语句正在评估" inner exception: " + e.InnerException == nulltruefalse It's all about operator precedence , which is why the second works just fine due to the parenthesis ( ( and ) ). 这都是关于运算符优先级的 ,这就是为什么第二个工作正常由于括号( () )。

See this reference for Operator Precedence . 有关操作员优先顺序,请参阅此参考 The + operator is evaluated before the equality == operator. 在等于==运算符之前计算+运算符。

It is due to operator precedence, in this case the + operator has higher precedence than the == operator so you need to use parenthesis to override the default precedence order so that the code is executed in the correct order. 这是由于运算符优先级,在这种情况下, +运算符的优先级高于==运算符,因此您需要使用括号来覆盖默认的优先顺序,以便以正确的顺序执行代码。

You can read all about it in C# language specification: 您可以在C#语言规范中阅读所有相关内容:

Operator precedence and associativity 运算符优先级和关联性

When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. 当表达式包含多个运算符时,运算符的优先级控制各个运算符的计算顺序。

This: 这个:

String s = " inner exception: " + e.InnerException == null ? "None" : e.InnerException.Message;

Is probably being evaluated like this: 可能正在评估如下:

String s = (" inner exception: " + e.InnerException) == null ? "None" : e.InnerException.Message;

Order of operations: 经营顺序:

String s = " inner exception: " + e.InnerException == null ? "None" : e.InnerException.Message;

Is evaluated as (" inner exception: " + e.InnerException) == null ? 被评估为(" inner exception: " + e.InnerException) == null ? which it's not. 它不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么会话对象抛出空引用异常? - Why does session object throw a null reference exception? 为什么这个嵌套对象初始化器抛出一个空引用异常? - Why does this nested object initializer throw a null reference exception? 为什么Silverlight View不会引发null异常? - Why does Silverlight View not throw a null exception? 为什么 HttpRequestMessage.Content.Headers.ContentType 会抛出 null 引用异常? - Why does HttpRequestMessage.Content.Headers.ContentType throw null reference exception? 什么时候遍历列表会引发Null Reference Exception? - When does looping through a list throw a Null Reference Exception? 为什么会抛出异常 - Why does this throw exception 为什么MVC在似乎没有Null引用时会抛出NullReferenceException? - Why does MVC throw a NullReferenceException when there seems to be no Null Reference? 如果Any()为true,为什么LINQ在Count()上抛出空引用错误? - Why does LINQ throw a null reference error on Count() if Any() is true? 为什么对 DataRow 空值的这种取消引用不会引发异常? - Why does this dereference of a DataRow null value not throw an exception? .Net 2+:为什么 if( 1 == null ) 不再引发编译器异常? - .Net 2+: why does if( 1 == null ) no longer throw a compiler exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM