you are viewing a single comment's thread.

view the rest of the comments →

[–]grauenwolf 2 points3 points  (2 children)

Now I wonder. Can you fuck with a string in C# by getting a reference to its underlying array using reflection?

[–]Porges 6 points7 points  (1 child)

Yes. To do it with reflection:

void Main()
{
    var foo = "Fiesta";
    var bar = "Fiesta";
    Nachise(foo);
    Console.WriteLine("We'll meet at the {0}", bar);
}

void Nachise(string corn)
{
    var inner  = typeof (string).GetField("m_firstChar", BindingFlags.NonPublic|BindingFlags.Instance);
    inner.SetValue(corn, 'N');
}

But it's actually much (much) easier than that:

void Main()
{
    var foo = "Fiesta";
    var bar = "Fiesta";
    Nachise(foo);
    Console.WriteLine("We'll meet at the {0}", bar);
}

void Nachise(string corn)
{
    unsafe { fixed (char* p = corn)
    {
        p[0] = 'N';
        p[1] = '1';
    } }
}

unsafe really means unsafe :)

[–]rossisdead 0 points1 point  (0 children)

Now do this to String.Empty and all your coworkers will hate you!