是否有与C#' S '?'操作符等价的VB.NET?

C#的??操作符是否与VB.NET等效?

72468 次浏览

使用带有两个参数的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.

您可以使用扩展方法。它的工作方式类似于SQLCOALESCE,对于您试图测试的内容来说可能是大材小用,但它可以工作。

    ''' <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多个参数。将返回第一个非NULL,并且在此之后不计算其余参数(短路,如AndAlso/&&OrElse/||

公认的答案没有任何解释,只是一个链接。
因此,我想我应该留下一个答案来解释If操作符是如何工作的,这个答案来自MSDN:


If运算符(Visual Basic)

使用短路计算有条件地返回以下两项之一 价值观。可以使用三个参数或两个参数调用如果运算符 参数.

If( [argument1,] argument2, argument3 )


使用两个参数调用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.


如果省略__abc0参数,则第一个参数必须为 引用或可空类型。如果第一个参数的计算结果为 返回第二个参数的值,__abc0。在所有其他情况下,返回第一个参数的值。这个 以下示例说明了此评估的工作方式。


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))

如何处理两个以上的值(嵌套ifS)的示例:

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)))

大多数这些解决方案的一个显著限制是它们不会短路。因此,它们实际上并不等同于??

内置的If操作符不会计算后面的参数,除非前面的参数计算为Nothing.

以下语句是等效的:

C#

var value = expression1 ?? expression2 ?? expression3 ?? expression4;

VB

dim value = if(expression1,if(expression2,if(expression3,expression4)))

这将在??起作用的所有情况下起作用。任何其他解决方案都必须极其谨慎地使用,因为它们很容易引入运行时错误。

请在此处查看有关IF运算符(Visual Basic)的Microsoft文档:https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator

If( [argument1,] argument2, argument3 )

下面是一些示例(VB.NET)

' 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"))