Strings in C# are not mutable. Any string operation is actually creating a brand new string and allocating it onto the heap. C# also doesn't provide any sort of tools for you to modify an existing string without creating a new one. Using some clever pointer magic, you can alter a string in place. This is not generally recommended, but it could be useful in some cases.

raw

class Program
{
    private const string HelloWorld = "Hello World";

    static void Main(string[] args)
    {
        Console.WriteLine(HelloWorld);
        Program.ChangeString(HelloWorld, "badflyer");
        Console.WriteLine(HelloWorld);

        const string HelloWorld2 = "Hello World 2";

        Console.WriteLine(HelloWorld2);
        Program.ChangeString(HelloWorld2, "not hello");
        Console.WriteLine(HelloWorld2);

        Console.ReadLine();
    }

    private static void ChangeString(string target, string newValue)
    {
        if(newValue.Length > target.Length)
        {
            throw new ArgumentException("The new string cannot be longer than the current string");
        }

        unsafe
        {
            fixed (char* str = target)
            {
                // Change the length to match our new string. The length is stored as an int right in front of the first character
                *((int*)str - 1) = newValue.Length;

                // Copy the new string
                for(var i = 0; i < newValue.Length; i++)
                {
                    str[i] = newValue[i];
                }
            }
        }
    }
}

Here's the output of this program:

raw

// Hello World
// badflyer
// Hello World 2
// not hello

Long story short, C# stores strings on the heap just as C would store a BSTR. It starts with a 32 bit length, and then the string as a fixed array of two byte chars. When you use the "fixed" expression in c#, you get a pointer to the first character of the string. We can step back 4 bytes to get a pointer to the string length, then we can change the string length to match our incoming string. You can't make the string any longer, because there is no way to know what follows in the heap.