title: Optimising binary serialization for multi-dimensional generic arrays
body: <p>I have a class that I need to binary serialize. The class contains one field as below:</p>
  
  <pre><code>private T[,] m_data;
  </code></pre>
  
  <p>These multi-dimensional arrays can be fairly large (hundreds of thousands of elements) and of any primitive type. When I tried standard .net serialization on an object the file written to disk was large and I think .net is storing a lot of repeated data about element types and possibly not as efficiently as could be done.</p>
  
  <p>I have looked around for custom serializers but have not seen any that deal with multi-dimensional generic arrays. I have also experimented with built-in .net compression on a byte array of the memory stream following serializing with some success, but not as quick / compressed as I had hoped.</p>
  
  <p>My question is, should I try and write a custom serializer to optimally serialize this array for the appropriate type (this seems a little daunting), or should I use standard .net serialization and add compression?</p>
  
  <p>Any advice on the best approach would be most appreciated, or links to resources showing how to tackle serialization of a multi-dimensional generic array - as mentioned <a href="http://www.codeproject.com/KB/dotnet/FastSerializer.aspx" rel="noreferrer">existing examples</a> I have found do not support such structures.</p>