Привет, ребята, Можем ли мы использовать оператор Coalesce (??) и условный тернарный оператор (:) в VB.NET, как в С#?
Ответ 1
Я думаю, вы можете приблизиться к использованию инструкции inline if:
//C#
int x = a ? b : c;
'VB.Net
Dim x as Integer = If(a, b, c)
Ответ 2
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
Ответ 3
только для справки, оператор Coalesce для 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