tags:

views:

548

answers:

3

Duplicate of http://stackoverflow.com/questions/126781/c-union-in-c

Is there a C# equivalent to the C union typedef? What is the equivalent of the following in C#?

typedef union byte_array
{
    struct{byte byte1; byte byte2; byte byte3; byte byte4;};
    struct{int int1; int int2;};
};byte_array
+1  A: 

C# doesn't natively support the C/C++ notion of unions. You can however use the StructLayout(LayoutKind.Explicit) and FieldOffset attributes to create equivalent functionality. Note that this works only for primitive types like int and float.

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Explicit)]
struct byte_array
{
    [FieldOffset(0)]
    public byte byte1;

    [FieldOffset(1)]
    public byte byte2;

    [FieldOffset(2)]
    public byte byte3;

    [FieldOffset(3)]
    public byte byte4;

    [FieldOffset(0)]
    public short int1;

    [FieldOffset(2)]
    public short int2;
}
M. Jahedbozorgan
A: 

Using the StructLayout attribute, it would look a little like this:

    [StructLayout(LayoutKind.Explicit, Pack=1)]
    public struct ByteArrayUnion
    {
        #region Byte Fields union

        [FieldOffset(0)]
        public byte Byte1;

        [FieldOffset(1)]
        public byte Byte2;

        [FieldOffset(2)]
        public byte Byte3;

        [FieldOffset(3)]
        public byte Byte4;

        #endregion

        #region Int Field union

        [FieldOffset(0)]
        public int Int1;

        [FieldOffset(4)]
        public int Int2;

        #endregion
    }
Erich Mirabal
A: 

Your question does not specify what your purpose is. If you are looking to marshal the data to pinvoke, then the 2 above answers are correct.

If not, the you simply do:

class Foo
{
  object bar;
  public int Bar {get {return (int)bar; } }
  ...
}
leppie