Photo by Bozhin Karaivanov on Unsplash

The Empty String in C#

Michael Moreno
Nerd For Tech
Published in
3 min readSep 18, 2021

--

I recently posed the following question to my team:

Which representation of the empty string do we prefer for our project?:

A) ""

B) string.Empty

I brought this question up because we decided to use StyleCop Analyzers, which has a rule encouraging developers to prefer string.Empty over "". But rather than just blindly following what the linter mandates, I thought it would be better to get everyone’s opinion. I was asked what my opinion was, and while initially I didn’t feel strongly either way, I realized this was something I ought to have an opinion on. And one of the benefits of having an opinion is I get to write a blog post about it.

The warning StyleCop Analyzers emits for this violation is named SA1122. It’s placed under the category of “Readability Rules”. Other Readability Rules are concerned with whether a comment contains text, or whether a parameter follows a comma. These, and other rules of this type, check that “code is well-formatted and readable”. So fine, StyleCop must be telling us that string.Empty is more readable, because this is a Readability Rule, after all. But here’s where it gets weird. Regarding the usage of "", the rule’s description states:

This will cause the compiler to embed an empty string into the compiled code. Rather than including a…

--

--