加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 23:53:44 所属栏目:大数据 来源:网络整理
导读:为什么第一个if语句评估为true?我知道如果我使用“is”而不是“=”,那么它不会评估为true。如果我用“Foo”替换String.Empty,则不会将其求值为true。 String.Empty和“Foo”都有相同类型的String,所以为什么一个评估为true而另一个不是? //this evaluat
为什么第一个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。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读