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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/629036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:07:07  来源:igfitidea点击:

Coalesce operator and Conditional operator in VB.NET

vb.netconditional-operatornull-coalescing-operator

提问by user42348

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

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

Hi guys, Can we use Coalesce operator(??) and conditional ternary operator(:) in VB.NET as in C#?

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

回答by Nick Josevski

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

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

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

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

回答by Mina Luke

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

回答by ivan

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

回答by Neelavardhan

If should be IIf

如果应该是 IIf

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

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