title: C#: Convert List of simple structs to byte[]
body: <p>In C# 4.0, say I have  </p>
  
  <pre><code>List&lt;HSZPAIR&gt; myList
  </code></pre>
  
  <p><em>with three elements</em> where the HSZPAIR struct is defined by:</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  public struct HSZPAIR
  {
      public IntPtr hszSvc;
      public IntPtr hszTopic;
  }
  </code></pre>
  
  <p><strong>How do I create a byte array for the entire myList?</strong>  In C++, you could just cast as array of structs down to a byte array.  I'm not sure how to do that in C#.</p>
  
  <p>I'm using an old Windows API function in the DDEML library that requires a byte array and the number of elements in the array as arguments.  If you are interested in more background, the API function is:</p>
  
  <pre><code>[DllImport("user32.dll", EntryPoint="DdeCreateDataHandle", CharSet=CharSet.Ansi)]
      public static extern IntPtr DdeCreateDataHandle(int idInst, byte[] pSrc, int cb, int cbOff, IntPtr hszItem, int wFmt, int afCmd);
  </code></pre>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/ms648747(VS.85).aspx" rel="nofollow">Here is it's documentation on MSDN</a>. The pSrc argument is the byte array of HSZPAIR structs.  The size of the array is the cb argument.</p>