String interning in C#

Last week a coworker sent me an interesting piece of code:

  1.  
  2. const string a = "";
  3. const string b = "";
  4. const string c = "hello";
  5. const string d = "hello";
  6.  
  7. Console.WriteLine(ReferenceEquals(string.Empty, string.Empty)
  8.   ? "Not so surprising…"
  9.   : "Oh, the humanity!");
  10.  
  11. Console.WriteLine(ReferenceEquals(a, string.Empty)
  12.   ? "But I thought string was a reference type!"
  13.   : "Seems fair…");
  14.  
  15. Console.WriteLine(ReferenceEquals(a, b)
  16.   ? " But I thought string was a reference type!"
  17.   : " Seems fair…");
  18.  
  19. Console.WriteLine(ReferenceEquals(c, d)
  20.   ? " But I thought string was a reference type!"
  21.   : " Seems fair…");
  22.  

Running the program will, maybe a bit surprising, produce:

Not so surprising…
But I thought string was a reference type!
But I thought string was a reference type!
But I thought string was a reference type!

The reason to this is what is called String interning. The CLR basically holds a hashmap of all the strings in the program (edit: string literals), one entry per unique string. So two identical strings will always have the same reference even though they are defined in different places. String interning is mainly used to speed up string comparisons (no need to check char by char if they are identical, if they have the same reference they are equal), but also to reduce the memory footprint of the application.

Read more about string interning:
http://aspadvice.com/blogs/from_net_geeks_desk/archive/2008/12/25/String-Interning-in-C_2300_.aspx
http://en.wikipedia.org/wiki/String_interning

1 comment on “String interning in C#

  1. Mikael helldén

    N.B. This is true for string literals only (string literals = strings enclosed in double quotes). Try running the following example:

    
    static void Main(string[] args)
    {
         const string c = "hello";
         var d = args[0];
    
         Console.WriteLine(ReferenceEquals(c, d)
                                      ? " But I thought string was a reference type!"
                                      : " Seems fair…");
    }
    
    

    with “hello” as (the first) command line parameter and you will see the difference.

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>