Re: empty vs null

From: Matthew (matthew_wills_at_mlc.com.au)
Date: 03/31/04


Date: 30 Mar 2004 17:29:10 -0800

Bill,

Re:

> VB.NET simplifies a lot of this, by allowing you to check for both conditions
> using the equality operator. So code like If MyString = Nothing, checks for
> both empty and null string conditions.

My docs (VB.NET 1.0) state:

<quote>
By definition, any String, including the empty string (""), compares
greater than a null reference (Nothing in Visual Basic); and two null
references compare equal to each other.

Use the Compare and Equals methods to perform combined reference and
value comparisons of Object and String instances. The equality and
inequality relational operators are implemented with the Equals method
and can also be used to make reference and value comparisons.
</quote>

which seems to contradict your point above (though feel free to point
out my error in understanding if you believe it is consistent with
your point). But when I TRY what you say, you appear to be right...

So, my question is, how did you find your above statement to be true?
Trial and error? Some other documentation elsewhere?

Thanks
Matthew

"Bill McCarthy <no spam>" <bill_mcc &#64; iprimus.com.au> wrote in message news:<O9R#KBUFEHA.3404@TK2MSFTNGP10.phx.gbl>...
> Hi Juzan,
>
> An empty string is "" where a null string is a variable that is yet to be
> allocated.
> To understand this, you really need to understand reference types, which is what
> a string is. A reference type, is a type when the actual location is pointed to.
> So say you have a variable MyString. That variable will be pushed onto the
> stack when you are working with it. The value of that will be a pointer to the
> string type itself, which will reside on the heap. IF the string is empty, "",
> there will be a pointer to a location on the heap where there is the empty
> string "" (or zero length string) If the string is null, then there will be a
> zero pointer, that is, no location on the heap.
>
> VB.NET simplifies a lot of this, by allowing you to check for both conditions
> using the equality operator. So code like If MyString = Nothing, checks for
> both empty and null string conditions.
> If you want to check for the reference actually being null, you use the Is
> operator. So If MyString Is Nothing checks for the null case only.
>
>
> Bill.
>
>
> "juzan" <chris@hotmail.com> wrote in message
> news:ux9ALpTFEHA.2560@TK2MSFTNGP12.phx.gbl...
> > hi
> > what is the difference between an empty string and a null string?
> > thanx
> >
> >



Relevant Pages

  • Re: C# Fundamentals Part 3: ReferenceEquals question
    ... |> take a look at the remark clausein the Framework reference guide. ... What is an "Empty ... String.Empty is a public static field of type String initialized ... empty string object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: newbie trouble making array of instances
    ... an Empty instance is initialized." ... string x = string.Empty; ... "" is not reference equal to string.Empty. ... The old instance of str1 was replaced with a whole new instance. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ref object in ArrayList
    ... You are not changing what is in the myString string object. ... object reference you would normally get). ... > The code compiles and runs just fine with the annoying exception that ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: reference type
    ... myString is passed to a method and it changes the value of the string.. ... reference and the second WriteLine should be "edited" instead of "hello ... you don't change the contents of the original string, but create a new string and changing the reference of the local variable myString to point to that new string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# Fundamentals Part 3: ReferenceEquals question
    ... Yes, but all Stringor Stringconstructors are special cases, ... take a look at the remark clausein the Framework reference guide. ... an Empty instance is initialized. ... x and y are references to the same string 'object', ...
    (microsoft.public.dotnet.languages.csharp)