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

2019-01-18 02:15发布

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't evaluate to true. Both String.Empty and "Foo" have the same type of String, so why does one evaluate to true and the other doesn't?

    //this evaluates to true
    If Nothing = String.Empty Then

    End If

    //this evaluates to false
    If Nothing = "Foo" Then

    End If

4条回答
Juvenile、少年°
2楼-- · 2019-01-18 02:42

Related to this topic, if you use a string variable initialized with "nothing" to be assigned to the property "value" of a SqlParameter that parameter is ignored, not included in the command sent to the server, and a missing parameter error is thrown. If you initialize that variable with string.empty everything goes fine.

//This doesn't work
Dim myString as String = nothing
mySqlCommand.Parameters.Add("@MyParameter", SqlDbType.Char).Value = myString

//This works    
Dim myString as String = string.empty
mySqlCommand.Parameters.Add("@MyParameter", SqlDbType.Char).Value = myString
查看更多
The star\"
3楼-- · 2019-01-18 02:45

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.

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-18 02:53

Try this:

Console.WriteLine("Is String.Empty equal to Nothing?: {0}", String.Empty.Equals(Nothing))

The = operator doesn't enforce equal types, whereas the .Equals() method of a string object does, as does the Is operator.

查看更多
疯言疯语
5楼-- · 2019-01-18 02:54

It's a special case of VB's = and <> operators.

The Language Specification states in Section 11.14:

When doing a string comparison, a null reference is equivalent to the string literal "".

查看更多
登录 后发表回答