.NET immutable string?

Content:

(concept from LinkedIn post)

Everyone know that .NET strings are immutable.

Everyone know that .NET has added a lot of features in recent years to make memory access efficient (Span etc.).

It seems those features have a side effect when combined with a very old feature in a creative way.

using System;
using System.Runtime.InteropServices;

namespace ModStr
{
    [StructLayout(LayoutKind.Explicit)]
    public ref struct StringHack
    {
        [FieldOffset(0)] public ReadOnlySpan<char> ro;
        [FieldOffset(0)] public Span<char> rw;
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            String s = "ABC";
            Console.WriteLine(s);
            StringHack h = new StringHack();
            h.ro = s;
            h.rw[1] = 'X';
            Console.WriteLine(s);
        }
    }
}

I don't see it as a real problem. It just mean that LayoutKind.Explicit must join unsafe { } in always generating a code review.

Comments: