Coalesce operator and Conditional operator in VB.NET [duplicate]

元气小坏坏 提交于 2019-11-28 11:57:21

I think you can get close with using an inline if statement:

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

'VB.Net
Dim x as Integer = If(a, b, c)
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

just for reference, Coalesce operator for 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
Neelavardhan

If should be IIf

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!