Skip to content

Instantly share code, notes, and snippets.

@alexzzzz
Last active September 25, 2020 07:03
Show Gist options
  • Save alexzzzz/c4299ca63aa2b19e9d1152a7fa04ee21 to your computer and use it in GitHub Desktop.
Save alexzzzz/c4299ca63aa2b19e9d1152a7fa04ee21 to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.InteropServices;
class Program
{
[StructLayout(LayoutKind.Explicit)]
struct Union
{
[FieldOffset(0)]
public string str;
[FieldOffset(0)]
public FakeString fakeStr;
}
class FakeString
{
public int length;
}
static void Main()
{
var union = default(Union);
union.str = "Hello, World!";
union.fakeStr.length = -42;
Console.WriteLine("Hello, World!".Length); // -42
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment