Nothing = String.Empty (Why are these equal?)
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.
![Justin Helgerson](https://i.stack.imgur.com/vbWjC.jpg?s=256&g=1)
Justin Helgerson
Updated on April 12, 2020Comments
-
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 about 14 yearsAre you using the
.Equals()
method to compare? -
Justin Helgerson about 14 yearsI 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 about 5 years
Nothing=String.Empty
butString.Empty != Nothing
. I just discovered this
-
-
Justin Helgerson about 14 yearsDo 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 about 14 yearsWhen 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 over 11 yearsWhat she means to say: VB.NET Nothing = default(T) in C#, not NULL
-
B Bulfin over 11 yearsBut isn't the default value for
String
Nothing
instead of""
? -
Bernhard Döbler over 10 yearsset value to DBNull.value instead of nothing
-
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 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 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 almost 8 yearsIt 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 over 7 years@RebeccaChernoff This answer is completely misleading:
default(String)
isnull
, notString.Empty
, sinceString
is a ref-type. The correct answer is the one from Heinzi, or in your comment aboutStrCmp
. Please modify your answer, for future reference. -
aelveborn about 5 yearsThis should be the accepted answer. The currently accepted answer is wrong and the correct answer is only given in its comments.
-
aelveborn about 5 yearsThere is no ambiguity between
Nothing
andString.Empty
in VB. They compare truthy, but they are not the same. PassingNothing
as the parameter value results in the parameter not being sent at all, same doesnull
in C#. PassingDBNull.Value
results in the databasenull
being sent as the parameter value, same happens in C#. Passing""
results in an empty string being sent to the database, same in C#.