21

可能重复:
VB.NET 中是否有条件三元运算符?

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

4

4 回答 4

19

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

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

'VB.Net
Dim x as Integer = If(a, b, c)
于 2009-03-10T05:52:18.327 回答
13
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
于 2011-03-06T10:19:18.140 回答
6

仅供参考,用于 String 的 Coalesce 运算符

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
于 2010-11-04T09:13:38.547 回答
-3

如果应该是 IIf

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

于 2010-09-16T17:01:14.920 回答