title: Marshal.SizeOf structure returns excessive number
body: <p>I have the following structure</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  public struct SFHeader
  {
  
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
      public string FileName;
  
      public int Offset;
  
      public short Size;
  
      public byte Flags;
  
      public byte Source;
  
  
      public long LastWriteTime;
  
  
      public byte[] GetBytes()
      {
          int size = Marshal.SizeOf(this);
          var buffer = new byte[size];
          IntPtr ptr = Marshal.AllocHGlobal(size);
  
          Marshal.StructureToPtr(this, ptr, true);
          Marshal.Copy(ptr, buffer, 0, size);
          Marshal.FreeHGlobal(ptr);
  
          return buffer;
      }
  
  
      public static SFHeader FromBytes(byte[] buffer)
      {
          var str = new SFHeader();
          int size = Marshal.SizeOf(str);
  
          IntPtr ptr = Marshal.AllocHGlobal(size);
          Marshal.Copy(buffer, 0, ptr, size);
          str = (SFHeader)Marshal.PtrToStructure(ptr, str.GetType());
          Marshal.FreeHGlobal(ptr);
  
          return str;
      }
  
  }
  </code></pre>
  
  <p>I need to convert my structure to an array of byte (to send as packet with socket), so I use the <code>GetBytes</code> method, but it returns an array of <code>24</code> bytes instead of an array of <code>21</code> bytes:</p>
  
  <ul>
  <li>Filename (string): 5 bytes</li>
  <li>Offset (int): 4 bytes</li>
  <li>Size (short): 2 bytes</li>
  <li>Flags (byte): 1 byte</li>
  <li>Source (byte): 1 byte</li>
  <li>LastWriteTime (long): 8 bytes</li>
  </ul>
  
  <p>So: 5+4+2+1+1+8 = 21 bytes.<br>
  This happens because <code>Marshal.SizeOf</code> returns 24, why?
  And it seems that the the bytes in excess are placed after the bytes of the string, in fact for example the following structure:</p>
  
  <pre><code>var header = new SFHeader()
  {
     FileName = "aaaa",
     Offset = 1,
     Size = 1
  };
  </code></pre>
  
  <p>is converted to the following buffer:</p>
  
  <pre><code>[0] = 97
  [1] = 97
  [2] = 97
  [3] = 97
  [4] = 0
  [5] = 0
  [6] = 0
  [7] = 0
  [8] = 1
  [9] = 0
  [10] = 0
  [11] = 0
  [12] = 1
  [13] = 0
  ... The following are all zero (0)
  </code></pre>
  
  <p>The fifth, sixth and seventh are the bytes in excess.
  How can I solve this problem?</p>