I've been running StyleCop over my code and one of the recommendations SA1122 is to use string.Empty rather than "" when assigning an empty string to a value.

My question is why is this considered best practice. Or, is this considered best practice? I assume there is no compiler difference between the two statements so I can only think that it's a readability thing?

UPDATE:
Thanks for the answers but it's been kindly pointed out this question has been asked many times already on SO, which in hind-sight I should have considered and searched first before asking here. Some of these especially forward links makes for interesting reading.

I'm sure this was asked and answered on Stack Overflow - but I'm having trouble finding it. It's to do with the fact that it has to instantiate a new string object for "" but not string.Empty
–
ChrisF♦Mar 19 '12 at 20:28

@ChrisF Ahh, didn't think to look on stack overflow for this type of question. I'll have a gander.
–
drezaMar 19 '12 at 20:30

I'd imagine there are two reasons, one for readability, and two for a minor performance boost.

Readability is easy: it's a lot quicker to spot string.Empty than "". Searching for string.Empty is also generally going to be easier and maybe more accurate than looking for the literal. It also clarifies intent: you didn't just make a typo or forget to finish something, you really did want empty string.

The performance reason is due to interning. You see, the runtime keeps a table of previously used strings so that it can quickly do string comparisons without actually doing a character-by-character check. string.Empty is already an interned reference to "" where-as typing the literal may not give you the interned version, thus causing a slight performance hit.

one nitpick, typing the literal "" will always give you the same interned version of "", at least since .Net 2 and up.
–
qesMar 19 '12 at 20:34

Right, the interning article on MSDN is a bit hard to follow on that point. It does seem like "" and string.Empty should both point at the same interned string, but it seems to indicate some differences between .net runtime versions. I think any string literal that appears multiple times in code will be interned on the second and further uses, yes?
–
CodexArcanumMar 19 '12 at 20:37

1

Also I should emphasize that when I say slight, I mean super-tiny. "Because of performance" is not a reason to go switching from one representation to the other, it's just a neat thing worth knowing about the runtime.
–
CodexArcanumMar 19 '12 at 20:38

2

It should also be noted that the performance aspect is only true for compilation. As both compile down to the same IL, the runtime performance should is identical.
–
lzcdMar 19 '12 at 22:06

that performance boost would only be on compile
–
ratchet freakMar 19 '12 at 22:31