VB.NET中的Coalesce运算符和条件运算符

47 浏览
0 Comments

VB.NET中的Coalesce运算符和条件运算符

这个问题已经在这里有了答案:

可能是重复问题:

是否有VB.NET中的条件三元运算符?

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

admin 更改状态以发布 2023年5月22日
0
0 Comments
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

的中文翻译为:包含粗体文本“123”的段落。

0
0 Comments

我认为你可以通过使用行内if语句来实现接近的效果:

//C#
int x = a ? b : c;
'VB.Net
Dim x as Integer = If(a, b, c)

0