title: string interning and referenceEquals
body: <p>I'm trying to understand string interning. Not for any real purpose other than learning.</p>
  
  <p>Here's where I'm at:</p>
  
  <p>Strings are immutable and a reference type. Its this immutability that allows us to do string interning.</p>
  
  <p>Without string interning, the two strings will be two strings on the heap.</p>
  
  <p>e.g. </p>
  
  <pre><code>private static void Main()
  {
     var a = "foo";
     var b = "foo";
     ReferenceEquals(a, b); // would expect this to be false...
  }
  </code></pre>
  
  <p>I would expect that <code>ReferenceEquals</code> to be false. It isn't though it's true. I thought to make it true I would have to do:</p>
  
  <pre><code>private static void Main()
  {
     var a = "foo";
     var b = "foo";
     ReferenceEquals(a, b); // false??
  
     string.Intern(a);
     string.Intern(b);
     ReferenceEquals(a, b); // true?
  }
  </code></pre>
  
  <p>Since the interning process, as I understand it, looks for the string in a hash table and if its not there it adds it. On further interning it looks for the string and if it finds it, then it changes the reference to point to the same place in the hash table.</p>
  
  <p>This should speed up comparisons? Since it it doesn't need to check if each char matches and can just check if both strings point to the same location. (Let's ignore the overhead of actually interning for now till I understand how this works).</p>
  
  <p>So what am I not getting. Why is the first code block returning true and not false?</p>