Visual Basic .NET Language
단락 연산자 (AndAlso - OrElse)
수색…
통사론
- result = expression1 그리고 또한 expression2
- 결과 = 표현식 1Elese 식 2
매개 변수
매개 변수 | 세부 |
---|---|
결과 | 필수 사항. 부울 식입니다. 결과는 두 표현식을 비교 한 부울 결과입니다. |
표현 1 | 필수 사항. 부울 식입니다. |
표현 2 | 필수 사항. 부울 식입니다. |
비고
'AndAlso' 와 'OrElse' 는 ShortCircuiting 연산자로, 컴파일러가 첫 번째 결과가 부울 비교의 부울 비교에있는 모든 표현식을 평가하지 않기 때문에 실행이 짧음을 의미합니다.
또한 용도
' 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.
기타 사용법
' 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
OrElse
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은 null 조건부 연산자를 도입 하여 예제의 AndAlso
버전의 동작을 모방 한 더 명확한 방식으로 함수를 다시 작성할 수 있습니다.
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow