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

vb.net – VB将字符串设置为空

发布时间:2020-12-17 00:11:20 所属栏目:大数据 来源:网络整理
导读:参见英文答案 Nothing = String.Empty (Why are these equal?)4个 Difference between C# and VB.Net string comparison2个 当我预期它返回FALSE时,下面的代码返回TRUE. 为什么它返回TRUE?我希望没有什么可以将字符串的值设置为null,而不是为空(根据msdn) C
参见英文答案 > Nothing = String.Empty (Why are these equal?)4个
> Difference between C# and VB.Net string comparison2个
当我预期它返回FALSE时,下面的代码返回TRUE.

为什么它返回TRUE?我希望没有什么可以将字符串的值设置为null,而不是为空(根据msdn)

CodeingGround sample

Module VBModule

    Sub Main()
        dim x as String
        x = nothing
        console.writeline(x = string.Empty)
    End Sub

End Module

Nothing (Visual Basic)

Represents the default value of any data type. For reference types,
the default value is the null reference.

***编辑****
Nothing = String.Empty (Why are these equal?)

VB.net中没有任何内容是类型的默认值.语言规范在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.

那时评论,

when converted to a particular type,it is the equivalent of the
default value of that type.

字符串的默认值为null.我不明白为什么这个答案被接受了.

Difference between C# and VB.Net string comparison

上面的帖子清楚地解释了答案,在评论部分提到Tim Schmelter找到上述帖子

Per Tim Schmeleters的评论

它是从vb编译器调用的,作为String.Equality Operator中的文档状态

(编辑:李大同)

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

    推荐文章
      热点阅读