0

Possible Duplicate:
C++ union in C#

#define AVEC3(T)    union { T v3[3]; struct { T x,y,z; }; }
#define AMAT3x3(T)  union { \
                    T v9[9], m3x3[3][3]; \
                    struct { T v3x[3], v3y[3], v3z[3]; }; \
                    struct { AVec3<T> vecx, vecy, vecz; }; \
                    struct { AVec3<T> right, up, back; }; \
                    struct { T xx, xy, xz, yx, yy, yz, zx, zy, zz; }; \

I have no idea how to convert the code from above(c++) into c# version. Assume T is double type.

Community
  • 1
  • 1
user976385
  • 117
  • 1
  • 10

1 Answers1

8

C# doesn't have a similar notion to union, but there is a trick to obtain the same functionality see more here where the FieldOffset attribute is used: C# equivalent to C "union"? from MSDN

From the MSDN post (simplified a bit):

using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Explicit)]
struct ByteArray {
  [FieldOffset(0)]
  public byte Byte1;
  [FieldOffset(0)]
  public int Int1;
  [FieldOffset(0)]
  public int Int2;
}
Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50