What is the difference, in practice, between "" and String.Empty?

Asked

Viewed 1,892 times

26

No. NET is noticeable in multiple ways to initialize a string with an empty value, called "quotation marks".

Is there a right way to do this? And what would be the practical difference between using:

  • var nome = String.Empty;
  • var nome = "";

I also know that you still have the string and String in C#, but was seen here that there is no difference in the use of the two.

2 answers

26


Zero practical difference. string.Empty is equivalent to "".

In the past there have even been, then some people recommended to use the "constant" and not the literal, but today each one uses what it finds more readable and eventually is compactuado with the team

I use "", because in some places the "constant" cannot be used (because technically it is not a constant but a field readonly). That goes for switch-case, default argument, attributes and any other location that requires a constant that must be fully resolved at compile time.

If you’re worried about performance, there’s a benchmark in the OS with IL code comparisons and Assembly.

More information can be obtained in the source code. And in the .NET Core.

To documentation explicitly says it is the same thing.

Because of the interning there is no creation of a new instance in either case. There is only one instance of string empty allocated in the static area of the application and all variables that have this value point to the same instance (the source is a response in Jon Skeet’s OS). He uses the flyweight pattern.

  • I’m not sure about that, but maybe the guy string in C# is also an object. There, in this case, string.Empty would be a predefined instance of an empty string, and so its attribution to a new string simply makes a copy of that instance. Whereas "" in fact it is in fact an empty string, and its assignment literally constructs a new instance from that "empty value" (as if it were "received as parameter" in the constructor). I thought it was worth this (possible) detailing, although you’re absolutely right: zero practical difference. :)

  • 1

    @Luizvieira updated showing that this does not happen in practice.

  • Very good part about the interning. I didn’t know. :)

14

There is really no difference from the point of view of performance and generated code. In performance tests, they went there and here, among which one was faster against the other, and only by milliseconds.

In looking behind the code, you don’t really see any difference either. The only difference is in the IL, that string.Empty use the operation code ldsfld and "" uses the operation code ldstr, but that’s only because string.Empty is static, and both instructions do the same thing.

If you look at the whole of what is produced, it is exactly the same.

One difference is that if you use in the syntax of a switch-case, you can’t write case string.Empty: because it is not a constant and you will have a Compilation error : A constant value is expected.

As in the example below.

using System;

public class Program
{
    public static void Main()
    {
        string teste = "";

        switch (teste)
        {
             case "":
                Console.WriteLine("Case 1");
                break;
            case String.Empty:
                Console.WriteLine("Case 2");
                break;
        }           

    }
}

Compilation error (line 14, col 9): A Constant value is expected

So user whatever you find readable, though. It’s subjective and varies from person to person - so I suggest you find out what most people on your team use, and that everyone makes consistency.

Personally I think String.Empty easier to read.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.