[Mono-dev] About Sebastien blog "Gendarme gains XML support, more..."

Jonathan Gilbert 2a5gjx302 at sneakemail.com
Mon Sep 18 15:34:02 EDT 2006

At 01:32 PM 18/09/2006 -0300, Rafael Teixeira wrote:
>>From the high number of UseStringEmptyRule hits in the example report,
>I think we can add some magic to gmcs/mcs, so that when run with
>-optimize it could automatically generate:
>x = String.Empty;
>from any
>x = "";
>it finds. And after that we should compile all mono managed libs with
>optimization turned on.
>Just a bit of my craziness...  :)

I'm a bit confused by this suggestion. My understanding is that at assembly
load time, the JIT interns all string literals that are present in the
module, canonicalizing them, and that therefore the two statements above
are 100% identical, semantically at least. For instance,

class MainClass
  static void Main()
    string from_literal = "";
      ReferenceEquals(from_literal, string.Empty));

This sample program outputs "True". This automatic interning of strings is
crucial for string 'switch' blocks to work. Also, it seems to me that 'x =
""' will perform better (on the order of a couple of clock cycles) than 'x
= string.Empty', since the former is resolved to a constant assignment (a
constant value, really, propagated to the use of variable 'x') at JIT time
while the latter, barring some special-case optimization in the JIT, will
get translated to a load from a static field.

I also happen to think that 'x = ""' is more readable than 'x =
string.Empty'. :-)

Do you know of a compelling reason to use 'x = string.Empty' over 'x = ""'?

Jonathan Gilbert

More information about the Mono-devel-list mailing list