title: Union in c# with StructLayout
body: <p>I have multiple structs that all starts with a header struct. Like this </p>
  
  <pre><code>public struct BaseProtocol {
      public Header header;
      public Footer footer;
  };
  </code></pre>
  
  <p>The header is </p>
  
  <pre><code>public struct Header {
      public Byte start;
      public Byte group;
      public Byte dest;
      public Byte source;
      public Byte code;
      public Byte status;
  };
  </code></pre>
  
  <p>The problem now is that I need to union them with a Byte[]. I tried it with this</p>
  
  <pre><code>[StructLayout( LayoutKind.Explicit, Size=255 )]
  public struct RecBuffer {
  
      [FieldOffset( 0 )]
      public Header header;
  
      [FieldOffset( 0 )]
      [MarshalAs( UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 255 )]
      public Byte[] buffer;
  };
  </code></pre>
  
  <p>When I fill the buffer with data I can't get the data from the header. How can I make c# do the same as I can do with union in c++?</p>