string.Empty, String.Empty and “”

As I was working the other day, I started thinking about the differences between String.Empty, string.Empty and “”. They are all empty strings but what are they? I have gotten nasty email in my noob to pro series about not cleaning it up earlier.

I think this is the first sign you need a girlfriend.

Alfred is often taxing. Anyway, I have found a couple of camps on this issue over the years. The first says there is no difference. Next comes the groups that say you create new objects with one and static references with the other. Finally, bring up the rear is the camp that says there is there is a different between the strings but not really one with the empty part of the equation. This is a tough nut to crack because it is two questions, wrapped into one.

So lets dig into this and unpack the questions.

String vs string

So here is the first thing. By my understanding, String is the type in the CLR and string is an alias to that type. So functionally they are identical. I believe the people who say they are radically different are people who used .NET 1.x. There seems to be a Gordian knot, I cannot untangle and my sword is in the shop on the .NET 1 question. At least since then, they compile the same in IL. Except, I think that is only technically true 99% of the time. We’ll talk about that in a moment.

I believe according to Microsoft, you should use string in most instances. When you need to reference the class, you should using String. I actually don’t do this.

The best post on why I stopped using String, in favor of string, is here. It was the thing that made me move completely away from any variable declared with the capital S and using anything String unless I cannot avoid it. It freaked me out when I realized he was right about String having no meaning.

If you are in the String camp then read the author that says you are right here.

There are basically two camp and it is a matter of drinking the Kool-Aid of your preferred camp. If you say no to both then do what Microsoft tells you. It’s always a defensible decision.

string.Empty or String.Empty vs “”

With the String vs string out of the way, we can look at the real question (s)String.Empty vs “”. If we went into the text books, we would see that “” is a magic number and magic numbers are bad. So that ends the debate. Use (s)String.Empty.

Does that feel unsatisfying to you? It kind of doesn’t to me too. What can I say, this is one of those in Programming 101 no-no’s, though a bunch of people ignore it in my experience.

If you want something more tangible, the only thing I have ever found on the topic is that “” may create an object while string.Empty is a reference so it does not.

I don’t agree because I don’t see it. It may have been an item in earlier versions of .NET but I know I get true when I run a message box comparing the two objects. My program says they are the same.

MessageBox.Show(object.ReferenceEquals(string.Empty, "").ToString());

Lets sum this up

If you are scared about String having no inherit meaning then use string everywhere.

string littleS = string.Empty;

If you are in the we should use CLR types camp then using String so String.Empty.

String bigS = String.Empty;

Finally, if you want to follow Microsoft because both cults are too weird for you then use String when you want to reference something about the class. Otherwise use string.

string msS = String.Empty;

If nothing else, try to keep it to one thing and not mix the styles. Your maintenance programmer will thank you.

Finally avoid “” as it is magic and computer scientists hate magic but that is a topic for another day.

I have no judgement on which path to take. Just have an idea on why.

Leave a Reply

Your email address will not be published.