String inside structure behavior <p>Let's say we have structure</p> <pre><code>struct MyStruct { public string a; } </code></pre> <p>When we assign it to the new variable what will be happened with the string? So for example, we expect that string should be shared when structs are copied in the stack. We're using this code to test it, but it returns different pointers:</p> <pre><code> var a = new MyStruct(); a.a = "test"; var b = a; IntPtr pA = Marshal.StringToCoTaskMemAnsi(a.a); IntPtr pB = Marshal.StringToCoTaskMemAnsi(b.a); Console.WriteLine("Pointer of a : {0}", (int)pA); Console.WriteLine("Pointer of b : {0}", (int)pB); </code></pre> <p>The question is when structs are copied in the stack and have string inside did it share the string or the string is recreated?</p> <p>[UPDATE]</p> <p>We also tried this code, it returns different pointers as well:</p> <pre><code> char charA2 = a.a[0]; char charB2 = b.a[0]; unsafe { var pointerA2 = &amp;charA2; var pointerB2 = &amp;charB2; Console.WriteLine("POinter of a : {0}", (int)pointerA2); Console.WriteLine("Pointer of b : {0}", (int)pointerB2); } </code></pre>