VB가 있나요?NET은 C#의 '?' 연산자에 상당합니까?
VB가 있나요?C#에 상당하는 NET??
오퍼레이터?
를 사용합니다.If()
두 개의 인수를 가진 연산자(Microsoft 문서):
' Variable first is a nullable type.
Dim first? As Integer = 3
Dim second As Integer = 6
' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))
second = Nothing
' Variable first <> Nothing, so the value of first is returned again.
Console.WriteLine(If(first, second))
first = Nothing second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))
그IF()
오퍼레이터가 다음 트릭을 수행합니다.
value = If(nullable, defaultValueIfNull)
http://visualstudiomagazine.com/listings/list.aspx?id=252
받아들여진 답변은 아무런 설명도 없고 단순히 링크일 뿐이다.
그래서 저는 이 질문에 대한 답을 남겨야겠다고 생각했습니다.If
MSDN에서 가져온 연산자 작업:
If 연산자(Visual Basic)
단락 평가를 사용하여 2개의 값 중 하나를 조건부로 반환합니다.If 연산자는 3개의 인수 또는2개의 인수를 사용하여 호출할 수 있습니다.
If( [argument1,] argument2, argument3 )
연산자가 2개의 인수를 사용하여 호출된 경우
If에 대한 첫 번째 인수는 생략할 수 있습니다.이것에 의해, 2개의 인수만을 사용해 연산자를 호출할 수 있습니다.다음 목록은 If 연산자가 두 개의 인수를 사용하여 호출된 경우에만 적용됩니다.
부품.
Term Definition
---- ----------
argument2 Required. Object. Must be a reference or nullable type.
Evaluated and returned when it evaluates to anything
other than Nothing.
argument3 Required. Object.
Evaluated and returned if argument2 evaluates to Nothing.
Boolean 인수를 생략한 경우 첫 번째 인수는 참조 유형 또는 늘 유형이어야 합니다.첫 번째 인수가 [Nothing]으로 평가되면 두 번째 인수의 값이 반환됩니다.다른 모든 경우 첫 번째 인수 값이 반환됩니다.다음 예시는 이 평가의 구조를 나타내고 있습니다.
VB
' Variable first is a nullable type.
Dim first? As Integer = 3
Dim second As Integer = 6
' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))
second = Nothing
' Variable first <> Nothing, so the value of first is returned again.
Console.WriteLine(If(first, second))
first = Nothing
second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))
세 개 이상의 값(내포)을 처리하는 방법의 예if
s) :
Dim first? As Integer = Nothing
Dim second? As Integer = Nothing
Dim third? As Integer = 6
' The LAST parameter doesn't have to be nullable.
'Alternative: Dim third As Integer = 6
' Writes "6", because the first two values are "Nothing".
Console.WriteLine(If(first, If(second, third)))
확장 방법을 사용할 수 있습니다.이것은 SQL과 같이 동작합니다.COALESCE
테스트하려는 것에 대해 과잉 살상일 수도 있지만 효과가 있습니다.
''' <summary>
''' Returns the first non-null T based on a collection of the root object and the args.
''' </summary>
''' <param name="obj"></param>
''' <param name="args"></param>
''' <returns></returns>
''' <remarks>Usage
''' Dim val as String = "MyVal"
''' Dim result as String = val.Coalesce(String.Empty)
''' *** returns "MyVal"
'''
''' val = Nothing
''' result = val.Coalesce(String.Empty, "MyVal", "YourVal")
''' *** returns String.Empty
'''
''' </remarks>
<System.Runtime.CompilerServices.Extension()> _
Public Function Coalesce(Of T)(ByVal obj As T, ByVal ParamArray args() As T) As T
If obj IsNot Nothing Then
Return obj
End If
Dim arg As T
For Each arg In args
If arg IsNot Nothing Then
Return arg
End If
Next
Return Nothing
End Function
빌트인If(nullable, secondChoice)
처리할 수 있는 것은 두 가지 null 선택뿐입니다.자, 할 수 있다Coalesce
필요한 만큼 파라미터를 설정할 수 있습니다.첫 번째 null 이외의 파라미터는 반환되며 나머지 파라미터는 그 후 평가되지 않습니다(단락 등).AndAlso
/&&
그리고.OrElse
/||
)
이러한 솔루션의 중요한 한 가지 제한은 단락되지 않는다는 것입니다.따라서 그것들은 실제로 동등하지 않다.??
.
기본 제공 연산자는 이전 매개 변수가 0으로 평가되지 않는 한 후속 매개 변수를 평가하지 않습니다.
다음 문장은 동일합니다.
C#
var value = expression1 ?? expression2 ?? expression3 ?? expression4;
VB
dim value = if(expression1,if(expression2,if(expression3,expression4)))
이것은, 다음의 경우에 유효합니다.??
다른 솔루션은 런타임 버그가 발생하기 쉽기 때문에 매우 주의해야 합니다.
If Operator(Visual Basic)에 관한 Microsoft 의 메뉴얼을 참조해 주세요.https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator
If( [argument1,] argument2, argument3 )
다음은 몇 가지 예입니다(VB).네트워크)
' This statement prints TruePart, because the first argument is true.
Console.WriteLine(If(True, "TruePart", "FalsePart"))
' This statement prints FalsePart, because the first argument is false.
Console.WriteLine(If(False, "TruePart", "FalsePart"))
Dim number = 3
' With number set to 3, this statement prints Positive.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))
number = -1
' With number set to -1, this statement prints Negative.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))
언급URL : https://stackoverflow.com/questions/403445/is-there-a-vb-net-equivalent-for-cs-operator
'programing' 카테고리의 다른 글
콘솔에 유니코드 문자를 쓰는 방법 (0) | 2023.04.07 |
---|---|
저장 프로시저를 만들기 전에 저장 프로시저가 있는지 확인하는 방법 (0) | 2023.04.07 |
SQL-Server: 오류 - 데이터베이스가 사용 중이므로 배타적 액세스 권한을 얻을 수 없습니다. (0) | 2023.04.07 |
SQL Server Management Studio, 실행 시간을 밀리초로 단축하는 방법 (0) | 2023.04.07 |
Microsoft SQL Server에 MySQL과 같은 Boolean 데이터 유형이 있습니까? (0) | 2023.04.07 |