VB.NET中的合并运算符和条件运算符

Coalesce operator and Conditional operator in VB.NET

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Is there a conditional ternary operator in VB.NET?

嗨,伙计们,我们可以使用coalesce运算符(??)和条件三元运算符(:)在vb.net中,如在c?


我认为您可以使用内联if语句:

1
2
3
4
5
//C#
int x = a ? b : c;

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


1
2
3
4
5
6
7
8
9
10
11
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


仅供参考,字符串的coalesce运算符

1
2
3
4
5
6
7
8
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

如果应该是IIF

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