Bab*_*k A 6 vb.net object nothing
为什么会有所不同?!
Public Class Form1
 Public Function MyFunction() As Integer?
    Return Nothing
 End Function
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim o As Object = Me
    MsgBox(TypeName(Me)) ' Form1
    MsgBox(TypeName(o))  ' Form1
    MsgBox(TypeName(Me.MyFunction())) ' Nothing
    MsgBox(TypeName(o.MyFunction()))  ' Nothing
    ' but
    MsgBox(TypeName(Me.MyFunction() + 0)) ' Nothing
    MsgBox(TypeName(o.MyFunction() + 0))  ' Integer
 End Sub
End Class
Visual Basic .NETNothing很早之前就有了可空值类型 - 它继承自 .NET 之前的 Visual Basic。default(T)在某些情况下,它的行为比t更像 C# null。
最后的调用是调用AddObjectVisual Basic 编译器服务中的方法。此方法已经存在很长时间了,并且再次早于可为空值类型,但不幸的是没有很好的记录。
不幸的是,他们无法使可空类型的行为绝对一致,尤其是在面对后期绑定调用时,同时仍然保持向后兼容性。例如,这还打印0:
Console.WriteLine(CType(CType(Nothing, Object), Int32))