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

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

嗨,大家好,我们可以在C#中使用VB.NET中的Coalesce运算符(??)和条件三元运算符(:)吗?


我想你可以使用内联if语句来关闭它:

//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

仅供参考,合并字符串操作符

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
链接地址: http://www.djcxy.com/p/42837.html

上一篇: Coalesce operator and Conditional operator in VB.NET

下一篇: "Response received is incomplete"