Is there a VB.NET equivalent for C#'s '??' operator?
C的
使用带两个参数的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ' 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)) |
1 | value = If(nullable, defaultValueIfNull) |
http://visualtudiomagazine.com/listings/list.aspx?ID=252
接受的答案没有任何解释,只是一个链接。因此,我想我会留下一个答案来解释
Uses short-circuit evaluation to conditionally return one of two
values. The If operator can be called with three arguments or with two
arguments.
1 If( [argument1,] argument2, argument3 )
< BR>
用两个参数调用的if运算符The first argument to If can be omitted. This enables the operator
to be called by using only two arguments. The following list applies
only when the If operator is called with two arguments.
< BR>
部分1 2 3 4 5 6 7 8 9 | 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. |
< BR>
When the Boolean argument is omitted, the first argument must be a
reference or nullable type. If the first argument evaluates to
Nothing, the value of the second argument is returned. In all other cases, the value of the first argument is returned. The
following example illustrates how this evaluation works.
< BR>
VB1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ' 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)) |
如何处理两个以上值(嵌套
1 2 3 4 5 6 7 8 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | ''' <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"运算符不会计算后续参数,除非早期参数的计算结果为Nothing。
以下陈述是等效的。
C.*
1 | var value = expression1 ?? expression2 ?? expression3 ?? expression4; |
VB
1 | dim value = if(exression1,if(expression2,if(expression3,expression4))) |
这在任何情况下都会起作用,在哪里?"工作。其他任何解决方案都必须非常小心地使用,因为它们很容易引入运行时错误。
查看有关if operator(Visual Basic)的Microsoft文档,网址为:https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator
1 | If( [argument1,] argument2, argument3 ) |
下面是一些例子(vb.net)
1 2 3 4 5 6 7 8 9 10 11 12 13 | ' 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")) |