- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为清楚起见编辑:这是编译器的一个非常奇怪的行为,我问的是为什么它通常会以这种方式运行,而不是如何解决它(已经有几个简单的解决方案)。
最近,我遇到了一段代码,它抛出了一个微妙的错误,最终抛出了一个异常。一个缩短的、人为的例子:
Dim list As List(Of Integer) = Nothing
If list?.Any() AndAlso list.First() = 123 Then
MessageBox.Show("OK then!")
End If
list
只是偶尔
Nothing
,为了清楚起见,我只是缩短了代码。
If
中第一项的意图语句是为了检查列表是否不是
Nothing
,并且还测试至少一个元素的存在。因为在这种情况下,
list
是
Nothing
,
list?.Any()
实际上/通常评估为
Nothing
.有点违反直觉,第二项,即
list.First() = 123
是
还有由运行时评估,导致明显异常。这有点违反直觉,因为起初猜测大多数人会想象
Nothing
好像是
False
,因为我们使用的是
AndAlso
在这里,短路运算符将阻止
If
的第二半语句从执行。
If list?.Any() Then
好像治疗
list?.Any()
作为
False
:
Dim list As List(Of Integer) = Nothing
If list?.Any() Then
MessageBox.Show("OK then!") 'This line doesn't get hit / run
End If
If list IsNot Nothing AndAlso list.Any() AndAlso list.First() = 123 Then
会工作得很好,就像
If If(list?.Any(), False) AndAlso list.First() = 123 Then
一样.
List<int> list = null;
if (list?.Any() && list.First() == 123)
{
MessageBox.Show("OK then!");
}
error CS0019: Operator '&&' cannot be applied to operands of type 'bool?' and 'bool'
最佳答案
这似乎是 VB 编译器语法评估中的一个遗漏(错误)。 ?. and ?() null-conditional operators (Visual Basic) 的文档状态:
Tests the value of the left-hand operand for null (Nothing) before performing a member access (?.) or index (?()) operation; returns Nothing if the left-hand operand evaluates to Nothing. Note that, in the expressions that would ordinarily return value types, the null-conditional operator returns a Nullable.
list?.Any()
(
Enumerable.Any Method ) 通常会返回
Boolean
(一个 ValueType),所以我们应该期待
list?.Any()
产生一个 Nullable(Of Boolean)。
list?.Any()
作为 Nullable(Of Boolean),它被视为记录在案。
If (list?.Any()).HasValue() AndAlso list.First = 123 Then
' something
End If
Dim source As List(Of Integer) = Nothing
Dim nullable As Boolean?
Dim nullable2 As Boolean? = nullable = If((Not source Is Nothing), New Boolean?(Enumerable.Any(Of Integer)(source)), Nothing)
nullable = If((nullable2.HasValue AndAlso Not nullable.GetValueOrDefault), False, If((Enumerable.First(Of Integer)(source) Is &H7B), nullable, False))
If nullable.GetValueOrDefault Then
MessageBox.Show("OK then!")
End If
Dim list As List(Of Integer) = Nothing
Dim nullable As Boolean?
Dim nullable2 As Boolean? = If(list IsNot Nothing,
New Boolean?(Enumerable.Any(Of Integer)(list)),
Nothing)
' nullable2 is nothing, so the 3rd line below is executed and throws the NRE
nullable = If((nullable2.HasValue AndAlso Not nullable.GetValueOrDefault),
False,
If((Enumerable.First(Of Integer)(list) = 123), nullable, False))
If nullable.GetValueOrDefault Then
MessageBox.Show("OK then!")
End If
AndAlso and OrElse, which use short-circuit evaluation, must evaluate their second operands when the first evaluates to Nothing.
Option Strict Off
,则此语句有意义已生效并使用
OrElse Operator如
Nothing
可以隐式转换为
False
.对于
OrElse
运算符,仅当第一个表达式为
True
时才计算第二个表达式.在
AndAlso
的情况下运算符,如果第一个表达式是
True
,则不计算第二个运算符.
Option Strict On
的代码片段.
Dim list As List(Of Integer) = Nothing
Dim booleanNullable As Nullable(Of Boolean) = list?.Any()
Dim b As Boolean = (booleanNullable AndAlso list.First() = 123)
If b Then
' do something
End If
Option Strict Off
,不会生成编译器错误,但会发生相同的运行时错误。
AndAlso
运算符包含在
If-Then
中阻止编译器使用
Option Strict Off
处理空条件运算符的结果不管
Option Strict
的实际状态如何,类型转换放宽.
关于vb.net - 为什么这个 If-AndAlso 语句中的第二项会提前求值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53683970/
我是一名优秀的程序员,十分优秀!