Page view counter

string.Empty vs ""

Last post 09-30-2008 3:22 AM by Xiaoth. 15 replies.

Sort Posts:

  • string.Empty vs ""

    03-31-2006, 10:33 AM
    Locked
    • Loading...
    • tr55
    • Joined on 03-21-2006, 2:10 AM
    • Posts 3
    • Points 15

    Hello,

    I was wondering if there's any difference (from the compiler standpoint of view) whether I'm passing string.Empty or "" as parameter to the function.

    thx in advance

  • Re: string.Empty vs ""

    03-31-2006, 10:53 AM
    Locked
    • Loading...
    • StrongTypes
    • Joined on 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • Points 30,696
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    This is a pointless debate in the developer world, but "" creates an object instance while String.Empty does not. I generally tend to use String.Empty over "".

    HTH,
    Ryan

    Ryan Olshan
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: string.Empty vs ""

    03-31-2006, 4:58 PM
    Locked
    • Loading...
    • KraGiE
    • Joined on 08-16-2002, 3:11 PM
    • Orange County
    • Posts 2,890
    • Points 14,455
    Yeah.  String.Empty is a read-only that returns "".  Since "" is actually a value type, it'll create that object anyways, but yeah.  I tend to use String.Empty also.  I believe it's faster, but I really don't have the core internals to prove it.
    Kay Lee
    MySpace.com - http://www.myspace.com/kragie
    Infrastructure Group
    MySpace.com

    - Code to live, but Live to code.
  • Re: string.Empty vs ""

    04-06-2006, 11:26 AM
    Locked
    • Loading...
    • bitmask
    • Joined on 07-29-2003, 3:18 PM
    • Citizen of the Earth
    • Posts 1,222
    • Points 6,475
    • TrustedFriends-MVPs

    KraGiE:
      Since "" is actually a value type, it'll create that object anyways, but yeah.  I tend to use String.Empty also. 

    Actually, since it is still a string, it's a reference type ;)

    Scott
    http://www.OdeToCode.com/blogs/scott/
  • Re: string.Empty vs ""

    04-06-2006, 2:21 PM
    Locked
    • Loading...
    • Caddre
    • Joined on 06-23-2003, 9:53 AM
    • Indy
    • Posts 5,308
    • Points 26,551
    bitmask:

    KraGiE:
      Since "" is actually a value type, it'll create that object anyways, but yeah.  I tend to use String.Empty also. 

    Actually, since it is still a string, it's a reference type ;)

    Kay,

    This is the beauty of these forums because when you tell other people they are confused about something for  making a mistake you can make the same mistake, '''' is sometimes used by object developers to deal with ANSI SQL null  because as the CLR team's head Brad Abrams said Strings are reference types that thinks they are value types.  We are human.

    Kind regards,
    Gift Peddie
  • Re: string.Empty vs ""

    04-06-2006, 2:25 PM
    Locked
    • Loading...
    • KraGiE
    • Joined on 08-16-2002, 3:11 PM
    • Orange County
    • Posts 2,890
    • Points 14,455
    You're absolutely right.  I'm actually blushing.  haha  It's just one of those things that basically creates a shallow copy when entering methods.  It's one of those reference types that doesn't really pass around a reference, but rather creates a shallow copy so any changes in the called method doesn't refect the altered value in the calling method.

    If that makes sense.  :) 
    Kay Lee
    MySpace.com - http://www.myspace.com/kragie
    Infrastructure Group
    MySpace.com

    - Code to live, but Live to code.
  • Re: string.Empty vs ""

    04-06-2006, 2:29 PM
    Locked
    • Loading...
    • Caddre
    • Joined on 06-23-2003, 9:53 AM
    • Indy
    • Posts 5,308
    • Points 26,551
    Strings are Immutable that is the CS term for it.  LOL   And have a nice day.
    Kind regards,
    Gift Peddie
  • Re: string.Empty vs ""

    04-06-2006, 2:40 PM
    Locked
    • Loading...
    • KraGiE
    • Joined on 08-16-2002, 3:11 PM
    • Orange County
    • Posts 2,890
    • Points 14,455
    Caddre:
    Kay,

    This is the beauty of these forums because when you tell other people they are confused about something for  making a mistake you can make the same mistake, '''' is sometimes used by object developers to deal with ANSI SQL null  because as the CLR team's head Brad Abrams said Strings are reference types that thinks they are value types.  We are human.



    Caddre, that is the beauty of the forums.  The string being a reference type instead of value type is a common one, and it's something I did know.  I just jumped the gun and typed before thinking.  This is quite a bit different than being "confused".

    Also, I believe ANSI Sql's Nulls are totally different from an empty string.  Where in the world do people actually use "" to deal with ANSI SQL null?  I always thought "object developers" had objects defined to map efficiently and correctly to other objects.. being that "" in ANSI SQL is completely different than "<null>"

    Kay Lee
    MySpace.com - http://www.myspace.com/kragie
    Infrastructure Group
    MySpace.com

    - Code to live, but Live to code.
  • Re: string.Empty vs ""

    04-06-2006, 3:16 PM
    Locked
    • Loading...
    • Caddre
    • Joined on 06-23-2003, 9:53 AM
    • Indy
    • Posts 5,308
    • Points 26,551

    I will not dignify that with an answer.

    http://blogs.msdn.com/somasegar/archive/2005/08/11/450640.aspx

     

     

     

     

    Kind regards,
    Gift Peddie
  • Re: string.Empty vs ""

    04-06-2006, 6:34 PM
    Locked
    • Loading...
    • KraGiE
    • Joined on 08-16-2002, 3:11 PM
    • Orange County
    • Posts 2,890
    • Points 14,455
    what does int? have to do with ""?  a nullable type doesn't dignify anything in regards to using "" in place of an ANSI SQL's null value.. int? x = DBNull.Value returns Error    2    Cannot implicitly convert type 'System.DBNull' to 'int?'   So where are you going with this?

    edited:
    Caddre, did you read that blog?  It talks about nullable types that was introduced to c#2.0.  Meaning..

    int xyz;

    if (xyz != null) {
        // this was not possible in previous versions of .net
    }

    It's to remove the need to explicitly set a value to value types.

    Kay Lee
    MySpace.com - http://www.myspace.com/kragie
    Infrastructure Group
    MySpace.com

    - Code to live, but Live to code.
  • Re: string.Empty vs ""

    04-07-2006, 4:51 AM
    Locked
    • Loading...
    • Xiaoth
    • Joined on 07-28-2002, 11:44 PM
    • Scottsdale, Arizona
    • Posts 17
    • Points 84
    StrongTypes:

    This is a pointless debate in the developer world, but "" creates an object instance while String.Empty does not. I generally tend to use String.Empty over "".

    HTH,
    Ryan

    Why is it pointless? String.Empty is a whole lot more typing than "".
     
    There is a performance difference but it's pretty minor. In the tests below, the "" operations loop took up 15.85 to 15.88 seconds, while the String.Empty operations loop took up 15.82 to 15.87 seconds.
     
            private void button3_Click(object sender, EventArgs e)
            {
                DateTime dt = DateTime.Now;
                string s = "";
                for (int i = 0; i < 1000000000; i++)
                {
                    if (s == "")
                    {
                        s = "";
                    }
                }
                TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - dt.Ticks);
                textBox1.Text = ts.ToString();
            }
     
            private void button4_Click(object sender, EventArgs e)
            {
                DateTime dt = DateTime.Now;
                string s = String.Empty;
                for (int i = 0; i < 1000000000; i++)
                {
                    if (s == String.Empty)
                    {
                        s = String.Empty;
                    }
                }
                TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - dt.Ticks);
                textBox1.Text = ts.ToString();
            }
  • Re: string.Empty vs ""

    04-07-2006, 8:58 AM
    Locked
    • Loading...
    • StrongTypes
    • Joined on 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • Points 30,696
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    Xiaoth:
    Why is it pointless? String.Empty is a whole lot more typing than "".
    There is a performance difference but it's pretty minor.
     
    1) I've read this too many times, but nobody has the research to proove it when they claim it.
    2) The difference is really minor. I can see how this might be important in a large scale project, but other than that it's like comparing apples and oranges.
     
    Ryan
    Ryan Olshan
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: string.Empty vs ""

    04-07-2006, 10:38 AM
    Locked
    • Loading...
    • nberardi
    • Joined on 06-14-2002, 4:58 AM
    • Phoenixville, PA
    • Posts 2,352
    • Points 11,233
    bitmask:

    KraGiE:
      Since "" is actually a value type, it'll create that object anyways, but yeah.  I tend to use String.Empty also. 

    Actually, since it is still a string, it's a reference type ;)

    However strings are immutable, which in a high level over view means they act like a value type.  In addition I also think the compiler checkes for other strings with the same signature to cut down on memory use.  I could be wrong on the previous statement, however all that being said I am still an advocate of String.Empty because it is cleaner and on the off chance an empty string ever changes to "*" my programs are all set.  Big Smile [:D]
    View My Blog Download My URL Rewriter and Reverse Proxy

    Only $9.95/month, ASP.NET, 2GB & SQL 2005
  • Re: string.Empty vs ""

    09-26-2008, 7:29 AM
    Locked
    • Loading...
    • devbrat_ghosh
    • Joined on 05-11-2007, 4:40 AM
    • New Delhi, India
    • Posts 45
    • Points 105

    Thanks to all contributors. This question arise several times . I took Ryan reply as answer of my long lasting doubt.

    with regards
    Dev

    I never lose, some people are just better than me at winning.
  • Re: string.Empty vs ""

    09-27-2008, 7:30 AM
    Locked
    • Loading...
    • TATWORTH
    • Joined on 02-04-2003, 8:34 AM
    • England
    • Posts 11,250
    • Points 57,407

    StyleCop requires string.Empty over an empty string. - Given its pedigree, I accept its advice in the matter.

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 2 (16 items) 1 2 Next >