Difference between String.Empty, Empty Quotes and String.length

Hi

Just a few days back I was working with string. At many point of time I had to make a checking for empty string. I started thinking about the difference between “” and string.empty. and which one is a better way to check if the string is empty or not.

The difference between string.Empty and “” is very small. String.empty will not create any object while “” will create a new object in the memory for the checking. Hence string.empty is better in memory management.

But the comparison with string.length == 0 will be even faster and the better way to check for the empty string. And I also have a bias for checking with string.length in this case. But do not forget to make a checking for null value if you are also expecting null value in the comparison.

4 thoughts on “Difference between String.Empty, Empty Quotes and String.length

  1. Each string literal does not necessarily result in a new string instance. When two or more string literals that are equivalent according to the string equality operator (§7.9.7) appear in the same assembly, these string literals refer to the same string instance. For instance, the output produced by
    class Test
    {
    static void Main() {
    object a = “hello”;
    object b = “hello”;
    System.Console.WriteLine(a == b);
    }
    }
    is True because the two literals refer to the same string instance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s