I detected a strange behavior in the on line If statement in VB.Net
If you check this code: jdoodle.com/a/X20
Imports System
Public Class Test
Public Shared Sub Main()
Dim x as Integer?
Dim ob1 As Objeto = New Objeto()
ob1.Valor = 1
Dim obnull As Objeto = Nothing
x = If(obnull Is Nothing, Nothing, obnull.Valor)
System.Console.WriteLine(x)
If Not obnull Is Nothing Then
x = obnull.Valor
Else
x = Nothing
End If
System.Console.WriteLine(x)
End Sub
End Class
Public Class Objeto
Public Valor As Integer
End Class
It returns 0
in the x = If(obnull Is Nothing, Nothing, obnull.Valor)
statement instead of a null value.
Why?