the difference between string.Empty and null

Discussion in 'ASP .Net' started by =?Utf-8?B?QXNoYQ==?=, Nov 2, 2004.

  1. 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.
     
    =?Utf-8?B?QXNoYQ==?=, Nov 2, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?QXNoYQ==?=

    bruce barker Guest

    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" <> wrote in message
    news:...
    > 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.
     
    bruce barker, Nov 2, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?QXNoYQ==?=

    Scott Allen Guest

    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"
    <> 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.
     
    Scott Allen, Nov 2, 2004
    #3
  4. 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:...
    > 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"
    > <> 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.

    >
     
    Kevin Spencer, Nov 2, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,515
  2. Replies:
    16
    Views:
    7,403
    Mike Schilling
    Oct 12, 2005
  3. Dennis Allison

    interpreting a null pointer as an empty (null string)

    Dennis Allison, Mar 5, 2004, in forum: C Programming
    Replies:
    11
    Views:
    825
    Derk Gwen
    Mar 6, 2004
  4. Rohn Farrington

    difference between "" and String.Empty

    Rohn Farrington, Feb 14, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    664
    John Saunders
    Feb 14, 2007
  5. Tzury Bar Yochay
    Replies:
    1
    Views:
    417
    Gabriel Genellina
    Mar 24, 2008
Loading...

Share This Page