C++ union in C#

You can use explicit field layouts for that:

[StructLayout(LayoutKind.Explicit)] 
public struct SampleUnion
{
    [FieldOffset(0)] public float bar;
    [FieldOffset(4)] public int killroy;
    [FieldOffset(4)] public float fubar;
}

Untested. The idea is that two variables have the same position in your struct. You can of course only use one of them.

More informations about unions in struct tutorial

Leave a Comment