Wednesday, March 28, 2012

string.empty

Hi,

When initializing newly declared variables is it any different to say

string myString = ""

versus

string myString = string.Empty;

If there is an advantage/disadvantage to one way over the other, what might that be?

Thanks

They're programatically identical. I think string.empty may be a few nanoseconds faster. You could always write a benchmarker and check how long it takes to assign 100,000 strings to "" as opposed to string.emptyand tell us about it :)


FXCOP advises that String.Empty should be used in preference to assigning an empty string.


This would summarize that:

http://codebetter.com/blogs/brendan.tompkins/archive/2003/10/14/2585.aspx

http://blogs.msdn.com/brada/archive/2003/04/22/49997.aspx


We've already spent more time discussing this than the entire world would save if it every application was instantly converted from "" to string.Empty. Wink


trying to understand the rules I read so far, a string doesn't actually get assigned a new value, but instead is destroyed and then a new one created with the new value.

So, from the links, it would seem that the string = "" would be destroyed when it was set to a new value, and since the string.Empty is never created as an object, there would be no destruction of it, prior to (re)assignment.

Anyway, sounds like it is trivial, so I won't worry about it.

0 comments:

Post a Comment