On 2005-09-21, Jon Skeet C# MVP <sk***@pobox.com> wrote:
Cor Ligthert [MVP] <no************@planet.nl> wrote: For your information in VB.Net is a difference between = Nothing and Is
Nothing.
= Nothing means for a value type empty or default
Is Nothing means for a reference type no reference.
By instance a string can have both
What makes a string different from other reference types though? That
doesn't make sense to me. Is it just to make it more like VB6?
Pretty much. It's to make string compares work in a way similar to the
way they did in VB6. String comparisons are special-cased in VB.Net not
just for the reference/value type/empty means nothing/ issue, but
because VB also needs to support things like the Option Compare
statement, and there's some automatic culture stuff that's handled
during string compares (I doubt that one in a thousand VB developers
could tell you what String = String actually does in any detail.).
In VB.Net, for a string s
If s Is Nothing
works identically to if(s == null)
If s = Nothing
is equivalent to if(s == null || s == String.Empty)
Note this isn't polymorphic, you can't do
Dim s As Object = "Hello World"
If s = Nothing
That won't compile, because "= Nothing" is not valid for reference
types, except for Strings.