微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐