Wednesday, March 24, 2010

Emulating Unions

It is possible to use attributes to emulate unions in C++, as shown in the following example:
[type: StructLayout(LayoutKind.Explicit)]
public struct UnionStruct
{
    [field: FieldOffset(0)]    //offset #0
    public int I;
    [field: FieldOffset(0)]    //offset #0
    public double d;
}
In this example, two attributes are used:
[type: StructLayout(LayoutKine.Explicit)]
[field: FieldOffset(0)]
The first attributes targets the struct, and the second targets a field of the struct. Notice that the words “type” and “field” in these attributes can be omitted because they are obvious.
Using these two attributes, it is possible to store two numbers, an int and a double, in the same memory location. The two numbers are different in size but they both start at offset 0. It is possible, of course, to use any offset, such as FieldOffset(2) or FieldOffset(5). It is also possible to store any number of different types in the same location as long as we don’t use them at the same time. In the following example, more types are added to the union.
//Example 7-8.cs
//Union emulation
using System;
using System.Runtime.InteropServices;
[StructLaout(LayoutKind.Explicit)]
public struct UnionStruct
{
    [FieldOffset(0)]
    public int i;
    [FieldOffset(0)]
    public double d;
    [FieldOffset(0)]
    public char c;
    [FieldOffset(0)]
    public byte b;
}
class MyClass
{
    static void Main()
    {
        unionStruct u = new UnionStruct();
        u.i = 13;
        Console.WriteLine(“Integer = {0}”, u.i);

        u.d = 12.34;
        Console.WriteLine(“Double = {0}”, u.d);
        u.c = (char)65;
        Console.WriteLine(“Character = {0}”, u.c);
        u.b = 127;
        Console.WriteLine(“Byte = {0}”, u.b);
       
    }
}

No comments:

Post a Comment