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
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.
Nothing in VB.net is the default value for a type. The language spec says in section 2.4.7:
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.
Try this:
The
=
operator doesn't enforce equal types, whereas the.Equals()
method of a string object does, as does theIs
operator.It's a special case of VB's
=
and<>
operators.The Language Specification states in Section 11.14: