Jus*_*son 25 vb.net string nothing
为什么第一个if语句评估为true?我知道如果我使用"是"而不是"="那么它将不会评估为真.如果我将String.Empty替换为"Foo",则它不会评估为true.String.Empty和"Foo"都有相同类型的String,那么为什么一个评估为true而另一个不评估?
//this evaluates to true
If Nothing = String.Empty Then
End If
//this evaluates to false
If Nothing = "Foo" Then
End If
Run Code Online (Sandbox Code Playgroud)
Reb*_*off 19
VB.net中没有任何内容是类型的默认值.该语言规范说,在节2.4.7:
没有什么是特殊的文字; 它没有类型,可以转换为类型系统中的所有类型,包括类型参数.转换为特定类型时,它等效于该类型的默认值.
因此,当您对String.Empty进行测试时,Nothing将转换为长度为0的字符串.Is运算符应该用于测试Nothing,而String.Empty.Equals(Nothing)也将返回false.