Fir*_*aad 147

使用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))
Run Code Online (Sandbox Code Playgroud)

  • 我认为VB中的`If()`语句与C#中的`if ...?...:`相同,而不是`??`运算符 (3认同)
  • @LukeTO'Brien 如果您将它与 3 个参数一起使用。如果您将它与 2 个参数一起使用,则更多的是“??”(请参阅​​此问题的另一个答案:http://stackoverflow.com/a/20686360/1474939) (3认同)
  • 这个答案显示了如何使用带有三个参数*的VB`If`*.这不是**类似于C#的`??`运算符.更好的答案是[Code Maverick's If with two arguments](http://stackoverflow.com/a/20686360/199364).(几年前,尼克有类似的答案,但不包括MSDN的解释.) (2认同)
  • 要了解以前的评论,请查看编辑历史记录。 (2认同)

Nic*_*ick 103

IF()运营商应该为你做的伎俩:

value = If(nullable, defaultValueIfNull)
Run Code Online (Sandbox Code Playgroud)

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

  • 该链接(有效)被破坏. (3认同)

Cod*_*ick 68

接受的答案没有任何解释,只是一个链接.
因此,我想我会留下一个答案,解释If运营商如何从MSDN中获取工作:


如果运算符(Visual Basic)

使用短路评估有条件地返回两个值中的一个.该如果操作员可以用三个参数或两个参数来调用.

If( [argument1,] argument2, argument3 )
Run Code Online (Sandbox Code Playgroud)


如果操作员使用两个参数调用

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.
Run Code Online (Sandbox Code Playgroud)


布尔省略参数,所述第一参数必须是参考或空类型.如果第一个参数的计算结果为 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))
Run Code Online (Sandbox Code Playgroud)

如何处理两个以上值(嵌套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)))
Run Code Online (Sandbox Code Playgroud)


Sti*_*ack 17

您可以使用扩展方法.这个就像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
Run Code Online (Sandbox Code Playgroud)

内置If(nullable, secondChoice)只能处理两个可空的选择.在这里,可以Coalesce根据需要获得尽可能多的参数.将返回第一个非空的,并且之后不评估其余参数(短路,如AndAlso/ &&OrElse/ ||)

  • 因为该语言有内置的运算符.没理由甚至看延伸方法. (8认同)
  • @Nick,对不起,但你完全错了.如果你有两个以上的coalesce参数,内置函数将不会删除它. (4认同)
  • 我不打算重复别人的回答.我认为,如果您需要使用单个语句检查多个值,那么提供备用解决方案可能会很好.既然它不是一个错误的答案,那么它应该被低估吗? (2认同)

use*_*825 10

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

除非先前的参数计算为空,否则内置的"if"运算符不会评估后续参数.

以下陈述是等效的.

C#

var value = expression1 ?? expression2 ?? expression3 ?? expression4;
Run Code Online (Sandbox Code Playgroud)

VB

dim value = if(exression1,if(expression2,if(expression3,expression4)))
Run Code Online (Sandbox Code Playgroud)

这将适用于"??"的所有情况 作品.任何其他解决方案都必须非常谨慎使用,因为它们很容易引入运行时错误.