是否有一个VB.NET等价于C#的'??' 运营商?

是否有一个VB.NET等价于C#的?? 运营商?

If()意思。 来自MSDN :

 If( [argument1,] argument2, argument3 ) 

当通过使用三个参数调用If() ,第一个参数必须求值为可以作为布尔值转换的值。 该布尔值将决定哪个另外两个参数被评估和返回。 以下列表仅适用于使用三个参数调用If运算符的情况。

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

IF()运算符应该为你做的伎俩:

 value = If(nullable, defaultValueIfNull) 

http://visualstudiomagazine.com/listings/list.aspx?id=252

被接受的答案没有任何解释,只是一个链接。
因此,我想我会留下一个解释If运算符如何从MSDN取得的解答:


如果运算符(Visual Basic)

使用短路评估来有条件地返回两个值中的一个。 If运算符可以用三个参数或两个参数来调用。

 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. 

当省略布尔参数时,第一个参数必须是引用或可为空的types。 如果第一个参数的计算结果为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一样工作,可能是你试图testing的东西,但它的工作原理。

  ''' <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 / ||

这些解决scheme大部分的一个重要限制是它们不会短路。 因此,他们实际上不等于?

内置的“if”运算符不会评估后续的参数,除非先前的参数计算为空。

以下声明是等同的。

C#

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

VB

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

这将在所有情况下“??” 作品。 任何其他解决scheme都必须谨慎使用,因为它们可能会引入运行时错误。