Is there a VB.NET equivalent for C#'s '??' operator?

Nathan Koop picture Nathan Koop · Dec 31, 2008 · Viewed 64.9k times · Source

Is there a VB.NET equivalent for C#'s ?? operator?

Answer

Firas Assaad picture Firas Assaad · Dec 31, 2008

Use the If() operator with two arguments (Microsoft documentation):

' Variable first is a nullable type.
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing
' Variable first <> Nothing, so the value of first is returned again. 
Console.WriteLine(If(first, second))

first = Nothing second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))