.net里 dbnull , nothing 关系...

来源:百度知道 编辑:UC知道 时间:2024/07/05 13:41:08
Dim s As String = Nothing
Dim b As System.DBNull

If IsDBNull(b) = True Then
MsgBox("null1")
End If

If s Is b Then
MsgBox("null2")
End If

测试下来,只报null2 ,而null1 却不报
这是为何?
IS 是比较两个引用类型,但为何却相等呢?

Dim b As System.DBNull 这句话并没有对b初始化。一个没有被初始化的引用类型在内存中存放的就是一个null值。所以b与a的引用是相等的。而b因为没有被初始化,所以null1不报。如果在Dim b As System.DBNull
这句之后加上 b = System.DBNull.Value 测试的结果就会是只报null1,而不报null2。