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

前端 未结 4 982
春和景丽
春和景丽 2020-12-01 15:58

Why does the first if statement evaluate to true? I know if I use \"is\" instead of \"=\" then it won\'t evaluate to true. If I replace String.Empty with \"Foo\" it doesn\

4条回答
  •  无人及你
    2020-12-01 16:32

    Nothing in VB.net is the default value for a type. The language spec says in section 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.

    So, when you test against String.Empty, Nothing is converted to a string, which has a length 0. The Is operator should be used for testing against Nothing, and String.Empty.Equals(Nothing) will also return false.

提交回复
热议问题