VB.NET中的Coalesce运算符和条件运算符

时间:2009-03-10 05:35:35

标签: vb.net conditional-operator null-coalescing-operator

  

可能重复:
  Is there a conditional ternary operator in VB.NET?

你好,伙计们,       我们可以在VB.NET中使用Coalesce运算符(??)和条件三元运算符(:),就像在C#中一样?

4 个答案:

答案 0 :(得分:18)

我认为你可以使用内联if语句来接近:

//C#
int x = a ? b : c;

'VB.Net
Dim x as Integer = If(a, b, c)

答案 1 :(得分:12)

Sub Main()
    Dim x, z As Object
    Dim y As Nullable(Of Integer)
    z = "1243"

    Dim c As Object = Coalesce(x, y, z)
End Sub

Private Function Coalesce(ByVal ParamArray x As Object())
    Return x.First(Function(y) Not IsNothing(y))
End Function

答案 2 :(得分:4)

仅供参考,Coalesce运算符为String

Private Function Coalesce(ByVal ParamArray Parameters As String()) As String
    For Each Parameter As String In Parameters
        If Not Parameter Is Nothing Then
            Return Parameter
        End If
    Next
    Return Nothing
End Function

答案 3 :(得分:-3)

如果应该是IIf

Dim x as Integer=IIf(a,b,c)