是否有VB.NET相当于C#中的 ??
运算符的等效操作符?
是否有VB.NET相当于C#中的 ??
运算符的等效操作符?
使用带有两个参数的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()
语句与C#中的if...?...:
相同,而不是??
运算符。 - Luke T O'Brien??
更多(请参见此问题的另一个答案:https://dev59.com/FHRC5IYBdhLWcg3wG9bp#20686360)。 - Brian JIf
语句。这与C#的??
运算符不同。更好的答案是Code Maverick的带有两个参数的If语句。(Nick在几年前也给出了类似的答案,但没有包含MSDN的解释。) - ToolmakerSteveIF()
操作符应该能满足你的需求:value = If(nullable, defaultValueIfNull)
被接受的答案没有任何解释,只是一个链接。
因此,我想留下一个答案,从MSDN中解释了If
运算符的工作原理:
Uses short-circuit evaluation to conditionally return one of two values. The If operator can be called with three arguments or with two arguments.
If( [argument1,] argument2, argument3 )
可以省略对If的第一个参数。这使得运算符可以仅使用两个参数进行调用。以下列表适用于仅使用两个参数调用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,则返回第二个参数的值。在所有其他情况下,返回第一个参数的值。以下示例说明了此评估如何工作。
' 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))
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)))
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)
只能处理两个可空选项。在这里,可以Coalesce
尽可能多的参数。将返回第一个非空参数,之后不会评估其余参数(短路,类似于AndAlso
/&&
和OrElse
/||
)
Return args.FirstOrDefault(Function(arg) arg IsNot Nothing)
:-) - Ulf Åkerstedt大多数这些解决方案的一个显著限制是它们不会短路。因此,它们实际上并不等同于 ??
。
内置的 If
运算符仅在前面的参数求值为无效时,才计算后续参数。
以下语句等效:
C#
var value = expression1 ?? expression2 ?? expression3 ?? expression4;
VB
dim value = if(expression1,if(expression2,if(expression3,expression4)))
这将适用于所有??
适用的情况。任何其他解决方案都必须极度谨慎地使用,因为它们可能轻易引入运行时错误。
请查看微软文档,了解If操作符(Visual Basic)的详细信息: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator
If( [argument1,] argument2, argument3 )
' 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"))