vb.net – Nothing = String.Empty(为什么这些相等?)

为什么第一个if语句评估为true?我知道如果我使用“is”而不是“=”,那么它不会评估为true。如果我用“Foo”替换String.Empty,则不会将其求值为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
VB.net中的任何内容都不是类型的认值。 language spec在第2.4.7节中说:

nothing is a special literal; it does not have a type and is convertible to all types in the type system,including type parameters. When converted to a particular type,it is the equivalent of the default value of that type.

所以,当你对String.Empty进行测试时,nothing被转换为一个长度为0的字符串。Is操作符应用于对nothing进行测试,String.Empty.Equals(nothing)也将返回false。

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...