Skip to content

Instantly share code, notes, and snippets.

@mrange
Last active May 14, 2023 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrange/774072703a534790f98bc1885ab7cdba to your computer and use it in GitHub Desktop.
Save mrange/774072703a534790f98bc1885ab7cdba to your computer and use it in GitHub Desktop.
Mutate C# strings
using System.Runtime.CompilerServices;
// An "immutable" string
var x = "I am immutable";
// *Insert evil laugh here*
var y = Unsafe.As<char[]>(x);
var z = "mutable!!";
// Overwriting the string
// Might only "work" on x64 but for hackers 'Might' is good enough ;)
for (int i = 0; i < z.Length; ++i) y[i+3] = z[i];
// Prints "I am mutable!!"
Console.WriteLine(x);
open System
open System.Runtime.CompilerServices
let x = "I am immutable";
let y = Unsafe.As<char[]>(x);
let z = "mutable!!";
for i = 0 to z.Length - 1 do y[i+3] <- z[i];
Console.WriteLine x
Imports System.Runtime.CompilerServices
Module Program
Sub Main(args As String())
Dim x = "I am immutable"
Dim y = Unsafe.As(Of Char())(x)
Dim z = "mutable!!"
For i = 0 To z.Length - 1
y(i + 3) = z(i)
Next
Console.WriteLine(x)
End Sub
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment