String.empty v.s. string.null

This is a discussion on String.empty v.s. string.null within the C# Programming forums, part of the General Programming Boards category; Hello everyone, I am doing input parameter checking for a string type, and string value except empty is valid. My ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    String.empty v.s. string.null

    Hello everyone,


    I am doing input parameter checking for a string type, and string value except empty is valid. My function is provided to outer client to call. Sometimes I find to check null is not enough, I also need to check String.Empty.

    My question is, what are the differences between null and String.Empty? I have made some web search for the answers but not very useful information.


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    String s;
    
    s = null;    // string is NULL. 
    
    s = "";     // string is empty.
    There seems to be a IsEmptyOrNull() function for String in managed code, perhaps that's what you want?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, I m only a C programmer, but looking at http://msdn.microsoft.com/en-us/libr...ing.empty.aspx
    it says that String.Empty is the same as "" (a blank string)

    So String.Empty is just the "" string and nothing more. null is...null

    Now, I suppose you cannot use null with String.Compare() or similar functions.

    EDIT: mastp was too fast
    EDIT2: Now I am curious. What is the REAL difference between NULL and "". If you assign null on a string it will have the null value. If you assign "" it will have what? A different value than null to indicate the differense?
    Last edited by C_ntua; 06-19-2008 at 09:06 AM.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It's easier to imagine if you see the variables as pointers (or references as the .NET term is). A null string is simply the pointer having a null value, the value 0. It doesn't point anywhere.
    Code:
    string Name = null;
    
    Name = 0x00000000
    An empty string is actually a string in memory of 0-length (with or without a null terminator im not sure). The pointer has a value, an adress to where the string is.
    Code:
    string Name = "";
    
    Name = 0x12345678
     |
     v
    
    +----+
    | \0 |
    +----+
    And a normal string is similar to an empty string, only there actually are some characters in memory.
    Code:
    string Name = "Hi";
    
    Name = 0x12345678
     |
     v
    
    +---+---+----+
    | H | i | \0 |
    +---+---+----+
    Last edited by Magos; 06-19-2008 at 09:20 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Magos explanation is correct , although I think he meant that a normal string is not like a null string as in, a normal string does point somewhere , whereas a null string doesnt point to anything ( unless you count 0x000 as something to point to then its completely correct).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SortedDictionary v.s. Dictionary
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 06-09-2008, 09:58 PM
  2. string v.s. basic_string
    By George2 in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2007, 12:23 PM
  3. VirtualAlloc v.s. malloc
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2007, 08:50 AM
  4. 32 bit v.s. 64 bit
    By xddxogm3 in forum Tech Board
    Replies: 4
    Last Post: 01-14-2005, 09:58 AM
  5. homework v.s. reuse
    By xddxogm3 in forum C++ Programming
    Replies: 2
    Last Post: 12-08-2003, 02:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21