191

C# 的运算符是否有 VB.NET 等价物??

4

6 回答 6

163

使用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))
于 2008-12-31T16:51:16.090 回答
109

操作员应该为IF()您解决问题:

value = If(nullable, defaultValueIfNull)

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

于 2008-12-31T16:53:06.710 回答
75

接受的答案没有任何解释,只是一个链接。
因此,我想我会留下一个答案来解释If操作员是如何工作的,取自 MSDN:


If 运算符 (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.


当省略布尔参数时,第一个参数必须是引用或可为空的类型。如果第一个参数的计算结果为 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))

如何处理两个以上值(嵌套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)))
于 2013-12-19T16:08:18.443 回答
20

您可以使用扩展方法。这一个像 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

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

于 2008-12-31T16:55:24.927 回答
13

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

除非前面的参数计算结果为空,否则内置If运算符不会计算后续参数。

以下语句是等效的:

C#

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

VB

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

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

于 2016-02-26T06:34:51.980 回答
3

在此处查看有关 If Operator (Visual Basic) 的 Microsoft 文档:https ://docs.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"))
于 2018-06-15T19:56:41.517 回答