Visual Basic .NET Language
短絡演算子(AndAlso - OrElse)
サーチ…
構文
- 結果=式1また、式2
- 結果=式1 OrElse式2
パラメーター
パラメータ | 詳細 |
---|---|
結果 | 必須。任意のブール式。結果は、2つの式の比較のブール結果です。 |
式1 | 必須。任意のブール式。 |
式2 | 必須。任意のブール式。 |
備考
'AndAlso'と'OrElse'はShortCircuit演算子であり、コンパイラは最初のものが偽の結果をもたらすならば、ブール比較のすべての式を評価しないので実行が短くなることを意味します。
また使用方法
' Sometimes we don't need to evaluate all the conditions in an if statement's boolean check.
' Let's suppose we have a list of strings:
Dim MyCollection as List(Of String) = New List(of String)()
' We want to evaluate the first value inside our list:
If MyCollection.Count > 0 And MyCollection(0).Equals("Somevalue")
Console.WriteLine("Yes, I've found Somevalue in the collection!")
End If
' If MyCollection is empty, an exception will be thrown at runtime.
' This because it evaluates both first and second condition of the
' if statement regardless of the outcome of the first condition.
' Now let's apply the AndAlso operator
If MyCollection.Count > 0 AndAlso MyCollection(0).Equals("Somevalue")
Console.WriteLine("Yes, I've found Somevalue in the collection!")
End If
' This won't throw any exception because the compiler evaluates just the first condition.
' If the first condition returns False, the second expression isn't evaluated at all.
OrElseの使用法
' The OrElse operator is the homologous of AndAlso. It lets us perform a boolean
' comparison evaluating the second condition only if the first one is False
If testFunction(5) = True OrElse otherFunction(4) = True Then
' If testFunction(5) is True, otherFunction(4) is not called.
' Insert code to be executed.
End If
NullReferenceExceptionを回避する
7.0
オルレース
Sub Main()
Dim elements As List(Of Integer) = Nothing
Dim average As Double = AverageElementsOrElse(elements)
Console.WriteLine(average) ' Writes 0 to Console
Try
'Throws ArgumentNullException
average = AverageElementsOr(elements)
Catch ex As ArgumentNullException
Console.WriteLine(ex.Message)
End Try
End Sub
Public Function AverageElementsOrElse(ByVal elements As IEnumerable(Of Integer)) As Double
' elements.Count is not called if elements is Nothing so it cannot crash
If (elements Is Nothing OrElse elements.Count = 0) Then
Return 0
Else
Return elements.Average()
End If
End Function
Public Function AverageElementsOr(ByVal elements As IEnumerable(Of Integer)) As Double
' elements.Count is always called so it can crash if elements is Nothing
If (elements Is Nothing Or elements.Count = 0) Then
Return 0
Else
Return elements.Average()
End If
End Function
7.0
また、
Sub Main()
Dim elements As List(Of Integer) = Nothing
Dim average As Double = AverageElementsAndAlso(elements)
Console.WriteLine(average) ' Writes 0 to Console
Try
'Throws ArgumentNullException
average = AverageElementsAnd(elements)
Catch ex As ArgumentNullException
Console.WriteLine(ex.Message)
End Try
End Sub
Public Function AverageElementsAndAlso(ByVal elements As IEnumerable(Of Integer)) As Double
' elements.Count is not called if elements is Nothing so it cannot crash
If (Not elements Is Nothing AndAlso elements.Count > 0) Then
Return elements.Average()
Else
Return 0
End If
End Function
Public Function AverageElementsAnd(ByVal elements As IEnumerable(Of Integer)) As Double
' elements.Count is always called so it can crash if elements is Nothing
If (Not elements Is Nothing And elements.Count > 0) Then
Return elements.Average()
Else
Return 0
End If
End Function
14.0
Visual Basic 14.0では、ヌルの条件付き演算子が導入され、この関数のAndAlso
バージョンの動作を模倣した、よりクリーンな方法で関数を書き直すことができます。
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow