3

Why is string.Empty more recommended than ""?

Is it because when the compiler is parsing the code and a " comes, the compiler will get ready to read a string? but in string.Empty the compiler will not even get ready to read a string?

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
Arrabi
  • 3,718
  • 4
  • 26
  • 38
  • Which language are we talking about? – Fred Foo Apr 13 '11 at 13:37
  • 2
    c#: http://blogs.msdn.com/b/brada/archive/2003/04/22/49997.aspx. "" creates an object while String.Empty references an existing object. – John Hoven Apr 13 '11 at 13:38
  • what would happen if "" no longer defines an empty string!! – Joe Apr 13 '11 at 13:39
  • 1
    And you can reference the Jon Skeet answer here: http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or/263257#263257 – John Hoven Apr 13 '11 at 13:39
  • @Joe, strings are immutable in C#. Once its an empty string, its always an empty string. – John Hoven Apr 13 '11 at 13:40
  • Ya I know that :) it was more of like why in C do we use NULL instead of 0 or ((void*)0). The benefits of string.Empty are rather low but I do use it myself. – Joe Apr 13 '11 at 13:43
  • I see what you're saying – John Hoven Apr 13 '11 at 13:52
  • 1
    @benjynito: You link to an article from 2003 before the literal `""` was interned. That is no longer the case: all uses of `""` point to the same object, so there is no difference. – Ed S. May 29 '11 at 00:14
  • Possible duplicate of [What is the difference between String.Empty and "" (empty string)?](http://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and-empty-string) – Liam Mar 16 '17 at 11:45
  • Possible duplicate of [In C#, should I use string.Empty or String.Empty or "" to intitialize a string?](http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or-to-intitialize-a-string) – slawekwin Mar 16 '17 at 12:12

4 Answers4

6

There's another reason.

Constants, because of their nature, are a Statics are references to single instances shared by all threads in some application domain, while a literal would end up in producing N instances of an empty string.

That's why the string.Empty constant read-only field is recommended over using the empty "" string literal, and obviously, as others have said, it increases readability.

Anyway, string interning should be taken in account, because under some conditions it might happen that two or more literals containing the same string could end up in a single instance (see remarks section on String.IsInterned docs):

The common language runtime automatically maintains a table, called the intern pool, which contains a single instance of each unique literal string constant declared in a program, as well as any unique instance of String you add programmatically by calling the Intern method.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • Since `Console.WriteLine(ReferenceEquals("", string.Empty));` returns true, I would suggest your edit is highly significant, and indeed shows that this answer is incorrect. And furthermore, `string.Empty` is NOT a constant, whereas `""` IS a constant - the opposite of what you are saying. Try doing `const string x = string.Empty;` and you'll get a compile error `The expression being assigned to 'x' must be constant`. If you do `const string x = "";` then you won't get an error. – Matthew Watson Jan 11 '17 at 09:08
  • @MatthewWatson Actually it's an answer from a long time ago. And you're right, it's not a constant but a read-only field... I'm going to fix this answer – Matías Fidemraizer Jan 11 '17 at 09:11
  • @MatthewWatson Now I'm at work, and I've tried to fix it quickly. BTW, do you find it better now? – Matías Fidemraizer Jan 11 '17 at 09:27
  • Well, it's better, but I disagree with the basic premise. The ONLY real reason to use `string.Empty` rather than `""` is because you find it more readable (I find it less readable, FWIW). `""` will ALWAYS end up with the same reference as `string.Empty`. – Matthew Watson Jan 11 '17 at 09:42
  • @MatthewWatson It's the same issue with `if(str?.trim() != "")` or `if(!string.IsNullOrEmpty(str))`, isn't it? You're right that, at the end of the day, it's a matter of taste... There're a lot of examples of our debate. – Matías Fidemraizer Jan 11 '17 at 09:51
  • 2
    I'm just saying that your statement that `a literal would end up in producing N instances of an empty string` is not correct. No mater how many literal `""` you have in an application, they will all reference the same `string.Empty`. – Matthew Watson Jan 11 '17 at 09:57
  • @MatthewWatson I'm going to completely fix tonight, I can't use more work time for this! :( – Matías Fidemraizer Jan 11 '17 at 09:59
  • I wouldn't worry about it too much - it's a really old answer, as you pointed out. – Matthew Watson Jan 11 '17 at 10:33
  • @MatthewWatson Meh but it's not about being old or new... StackOverflow is about quality. – Matías Fidemraizer Jan 11 '17 at 10:47
  • @MatthewWatson (and, in my case, I don't want to share incorrect info) – Matías Fidemraizer Jan 11 '17 at 10:48
4

No, it most certainly isn't the right answer. You don't mention a language, so let me guess some stuff here:

String.Empty is a constant defined on class string. "" is a string literal for the empty string.

Now, if you are doing equality comparisons, then you want to be sure you're talking about the same object, right?

Does your language guarantee that "" and string.empty compare equal? This could also be a question of the runtime. I think the term you want to google is string interning. If you have that, then it doesn't really matter which one you use. If you don't, well, subtle errors will occur.

EDIT: I see you are talking about c#. That does have string interning, so it doesn't really matter which one you use. This is just a matter of style.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • 1
    I believe low-level details aren't a good argument. I tend to take high-level facts, since I'm using a very high-level language such as C#. And in terms of code compilation, there's a great difference: string.Empty won't be compiling N times same "" string, although run-time low-level details such as string interning would optimize that. – Matías Fidemraizer Apr 13 '11 at 13:54
4

if you are using "" it can be easily mistaken with " " so to increase readability String.Empty; can be used

Liam
  • 27,717
  • 28
  • 128
  • 190
Nighil
  • 4,099
  • 7
  • 30
  • 56
  • That's why I never write lower case "l" or UpperCase "I" in my code and always use StringHelper.LowerCaseL and StringHelper.LowerCasei – dekajoo Oct 15 '18 at 14:00
0

It's basically like a constant value for empty that's more human readable.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • 5
    Readability for most cases is highly subjective metric. E.g. - I prefer `""`. – Arnis Lapsa Apr 13 '11 at 13:39
  • @Armis L. but coding standards are the definition of "code readability". We can argue string.Empty is better than "" or whatever, but if we need to follow coding standards, you'll be using string.Empty, fact that's going to increase your code readability because your code will be predictable. What do you think? :D – Matías Fidemraizer Apr 13 '11 at 13:57