Smithers said:
[...]
So, yes - I botched the OP here because the rule talks about
string.Empty
vs.
""
as opposed to string.empty vs null (which I put in the OP).
Anyway I've heard this rule from Jesse Lilberty as well. So give that at
least two well published authors speak of it, I wanted to understand the
reasoning behind it. Thus the OP here.
I discovered recently that in spite of string pooling, the literal ""
and String.Empty are not equivalent. I didn't explore it any further to
see if there was a way I could get "" and string.Empty to be pooled to
the same string, but the default behavior didn't appear to.
Since as far as I know, the string represented by string.Empty will
always exist, you can avoid having a new empty string added to your
string constant pool by always using that instead of "". However, I
doubt there's much different beyond that. As long as string pooling is
enabled, the worst that using "" instead of string.Empty should cause is:
1) the addition of a single extra string in the string constant pool
2) very slightly slower comparisons between one empty string and
another when they aren't using the same version of the empty string (I'm
assuming here that Equals() for strings checks for reference equality
first, so if the constants are the same reference, this would shortcut
the need to compare length and contents, speeding things very slightly).
In other words, I can't imagine that there's any truly significant
difference between the two. They aren't literally the same, at least in
some situations, but you are unlikely to ever notice the difference in a
real-world application.
Pete