Hi,
I'm looking for a way to use a union like in the example below.
The type fails to load for the obvious reason that the string is not a
value type, and so we have 3 fields of length 4, pointing to the strings on
the heap.
I would like the characters to be on the stack and pinned so the example
would work.
I've been playing around with stackalloc but to no avail.
Any input would be greatly appreciated.
Thanks.
Michel.
[StructLayout (LayoutKind.Explicit)]
class c
{
[FieldOffset (0)]
public string f1 = new string (' ', 5);
[FieldOffset (5)]
public string f2 = new string (' ', 3);
[FieldOffset (0)]
public string all = new string (' ', 8);
}
private void Form1_Load(object sender, System.EventArgs e)
{
c o = new c();
o.all = "12345678";
Debug.Assert (o.f1 == "12345");
Debug.Assert (o.f2 == "678");
}