title: Can you change the contents of a (immutable) string via an unsafe method?
body: <p>I know that strings are immutable and any changes to a string simply creates a new string in memory (and marks the old one as free). However, I'm wondering if my logic below is sound in that you actually can, in a round-a-bout fashion, modify the contents of a string. </p>
  
  <pre><code>const string baseString = "The quick brown fox jumps over the lazy dog!";
  
  //initialize a new string
  string candidateString = new string('\0', baseString.Length);
  
  //Pin the string
  GCHandle gcHandle = GCHandle.Alloc(candidateString, GCHandleType.Pinned);
  
  //Copy the contents of the base string to the candidate string
  unsafe
  {
      char* cCandidateString = (char*) gcHandle.AddrOfPinnedObject();
      for (int i = 0; i &lt; baseString.Length; i++)
      {
          cCandidateString[i] = baseString[i];
      }
  }
  </code></pre>
  
  <p>Does this approach indeed change the contents <code>candidateString</code> (without creating a new candidateString in memory) or does the runtime see through my tricks and treat it as a normal string?</p>