4

How should I initialize an empty string in C# and why? Which one is faster?

string myString = "";

or

string myString = "".ToString();
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Cris
  • 197
  • 1
  • 4
  • 16

4 Answers4

9

String.ToString() just returns the string itself :

    public override String ToString() {
        Contract.Ensures(Contract.Result<String>() != null);
        Contract.EndContractBlock();
        return this;
    }

Which means string myString = "".ToString() is pointless. After all, it's already a string

In fact, given that strings are immutable and, in this case, interned, "" is the same string instance returned by String.Empty

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • It also means "".ToString() is likely slightly slower, but to verify one would need to test it. It's possible it gets optimized by the compiler. – Tim Oct 18 '19 at 12:09
  • 1
    @Tim what it means, is that the code base is bad with blind copying all around and `ToString()` used to avoid casts – Panagiotis Kanavos Oct 18 '19 at 12:12
  • @Tim The JIT optimizes `"".ToString()` in Release. In all other cases, it is not optimized. – canton7 Oct 18 '19 at 12:12
  • 1
    There are quite a few questions in SO with people blindly using `ToString()` on a field value column only to *parse* the result back into a date or double. – Panagiotis Kanavos Oct 18 '19 at 12:14
6

The second one is redundant. It's already a string. So the first one would be the best option.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
Damon Quire
  • 130
  • 8
  • 1
    Many thanks for your answer, I've seen the second option in some code made by others and I was wondering which one to use. – Cris Oct 18 '19 at 12:04
  • No problem. Like another answer said, string.Empty is another option. They'll perform pretty much identically but some people prefer string.Empty from a code readability standpoint. – Damon Quire Oct 18 '19 at 12:07
2

Please use the first one which is the best option

string myString = "";

You can also use string.Empty in C#

string myString = string.Empty;

The second one is pointless because "" which is already a string and converting string("").ToString() is pointless.

Suhas Parameshwara
  • 1,344
  • 7
  • 15
  • 1
    why is the first one the best option? Please explain :) – Corentin Pane Oct 18 '19 at 12:05
  • 1
    One thing is: you cannot use the second one as an optional method parameter. The compiler will say it is not a compile time constant, with first - there is not such error. – Dimitar Oct 18 '19 at 12:07
  • 1
    Before .net 2.0 there was a slight difference and "" created a new object where string.Empty did not. This is no longer true and which one you use comes down to personal preference. I personally see "" used far more than string.Empty except in really old code. There are also cases where "" will work but string.Empty causes a compilation error. See: https://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and-empty-string (see the 2 top answers). – Tim Oct 18 '19 at 12:18
1

You can also use string.Empty which is more readable. It has nothing to do with performance.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
stud3nt
  • 2,056
  • 1
  • 12
  • 21