static void Main() { char[] chars = new StringToChar { str = constFoo }.chr; for (int i = 0; i < constFoo.Length; i++) { chars[i] = 'M'; Console.WriteLine(chars[i]); // Always prints "M". } Console.WriteLine("FOO"); // x86: Prints "MMM". x64: Prints "FOM". }
static void Main(string[] args) { const string test = "ABCDEF"; // Strings are immutable, right? char[] chars = new StringToChar { str = test }.chr; chars[0] = 'X'; // On an x32 release or debug build or on an x64 debug build, // the following prints "XBCDEF". // On an x64 release build, it prints "ABXDEF". // In both cases, we have changed the contents of 'test' without using // any 'unsafe' code... Console.WriteLine(test); // The following line is even more disturbing, since the constant // string "ABCDEF" has been mutated too (because the interned 'constant' string was mutated). Console.WriteLine("ABCDEF"); }