Saturday, March 24, 2012

the difference between string.Empty and null

greetings, i have some questions below, what are the differences between

private string _strVal = string.Empty; and _strVal = null;

does the string.Empty; allocate memory for it? how about _strVal = ""

thanks for enlightening me on this.string.Empty points to one string that equal "". due to literal pooling,
there is only one copy per assembly for "". null is a different value, and
takes no gc memory.

"Asha" <Asha@.discussions.microsoft.com> wrote in message
news:5AD99FFB-A480-4951-9F7D-E7DCBFE34453@.microsoft.com...
> greetings, i have some questions below, what are the differences between
> private string _strVal = string.Empty; and _strVal = null;
> does the string.Empty; allocate memory for it? how about _strVal = ""
> thanks for enlightening me on this.
To expand on Bruce's comment:

String.Empty is a valid instance of the string class, you can invoke
the Length property (and get back a value of 0). A null reference does
not refer to a valid object.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 1 Nov 2004 17:09:01 -0800, "Asha"
<Asha@.discussions.microsoft.com> wrote:

>greetings, i have some questions below, what are the differences between
>private string _strVal = string.Empty; and _strVal = null;
>does the string.Empty; allocate memory for it? how about _strVal = ""
>thanks for enlightening me on this.
To expand on Scott's comment:

A string is actually an array of char. The array is terminated with a null
zero character (ASCII 0 or \0). The null zero does not count when referring
to the length of the string. It is simply the terminator character for the
array. A null string is a variable that is not initialized and points to
null. An empty string is a variable that points to an array with only a null
zero in it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Scott Allen" <bitmask@.[nospam].fred.net> wrote in message
news:8n1eo052sbblhhqq4ao03tdaske5r869n5@.4ax.com...
> To expand on Bruce's comment:
> String.Empty is a valid instance of the string class, you can invoke
> the Length property (and get back a value of 0). A null reference does
> not refer to a valid object.
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
> On Mon, 1 Nov 2004 17:09:01 -0800, "Asha"
> <Asha@.discussions.microsoft.com> wrote:
> >greetings, i have some questions below, what are the differences between
> >private string _strVal = string.Empty; and _strVal = null;
> >does the string.Empty; allocate memory for it? how about _strVal = ""
> >thanks for enlightening me on this.

0 comments:

Post a Comment