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

33,305

Solution 1

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.

Solution 2

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 "".


If you are interested in further details, I have written an in-depth comparison of vbNullString, String.Empty, "" and Nothing in VB.NET here:

Solution 3

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

Solution 4

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.

Share:
33,305
Justin Helgerson
Author by

Justin Helgerson

Updated on April 12, 2020

Comments

  • Justin Helgerson
    Justin Helgerson about 4 years

    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
    
    • Amber
      Amber about 14 years
      Are you using the .Equals() method to compare?
    • Justin Helgerson
      Justin Helgerson about 14 years
      I added a code snippet. I don't actually need to do a comparison between null and an empty string, I am just curious as to why that statement evaluated to true.
    • dgo
      dgo about 5 years
      Nothing=String.Empty but String.Empty != Nothing. I just discovered this
  • Justin Helgerson
    Justin Helgerson about 14 years
    Do you know the reason behind automatically converting Nothing to a string on the fly when I do the comparison? What is the benefit of this?
  • Rebecca Chernoff
    Rebecca Chernoff about 14 years
    When using the = operator with a string, VB.NET uses StrCmp, rather than op_Equality. I'd speculate this was for backwards compatibility reasons.
  • Stefan Steiger
    Stefan Steiger over 11 years
    What she means to say: VB.NET Nothing = default(T) in C#, not NULL
  • B Bulfin
    B Bulfin over 11 years
    But isn't the default value for String Nothing instead of ""?
  • Bernhard Döbler
    Bernhard Döbler over 10 years
    set value to DBNull.value instead of nothing
  • jods
    jods over 10 years
    @recursive: Yes, it is. And that holds for VB as well. The main answer is incorrect and the real reason is given in the comment by Rebecca: VB.NET calls StrCmp when you compare strings with =. This method has special code to handle "" = Nothing as true. If you'd try "".Equals(Nothing) you'd get false.
  • Reinstate Monica
    Reinstate Monica over 9 years
    @Quandary: Nothing is the equivalent of Null, and if compared with the Is operator would not match String.Empty. It is only the = comparison (which does not compare references) that return true.
  • Stefan Steiger
    Stefan Steiger over 9 years
    @afuna: Except that default(string) = null, and not "". And no, nothing is the equivalent of default(T), which is not the same as NULL, that is, only if you have a non-nullable type, like GUID...
  • Gqqnbig
    Gqqnbig almost 8 years
    It points out a problem that if an external library is not written in VB, the author of the library might not be aware of the ambiguity between nothing and empty string in VB. So when it's used in VB code, users have to be carefull passing nothing or empty string.
  • Teejay
    Teejay over 7 years
    @RebeccaChernoff This answer is completely misleading: default(String) is null, not String.Empty, since String is a ref-type. The correct answer is the one from Heinzi, or in your comment about StrCmp. Please modify your answer, for future reference.
  • aelveborn
    aelveborn about 5 years
    This should be the accepted answer. The currently accepted answer is wrong and the correct answer is only given in its comments.
  • aelveborn
    aelveborn about 5 years
    There is no ambiguity between Nothing and String.Empty in VB. They compare truthy, but they are not the same. Passing Nothing as the parameter value results in the parameter not being sent at all, same does null in C#. Passing DBNull.Value results in the database null being sent as the parameter value, same happens in C#. Passing "" results in an empty string being sent to the database, same in C#.