title: Efficient array access as multiple types
body: <p>I am using entity framework and need to store some binary data. I am doing this with success using a byte array and some helper methods to return the data in other formats.</p>
  
  <p>I am doing this like so.</p>
  
  <pre><code>public class TestData
  {
      public double SampleRate { get; set; }
  
      public byte[] ExtensionBytes { get; set; }
  
      /// &lt;summary&gt;
      /// Provides easy access as floats instead of bytes
      /// &lt;/summary&gt;
      public float[] ExtensionValues
      {
          get
          {
              var result = new float[ExtensionBytes.Length / sizeof(float)];
              Buffer.BlockCopy(ExtensionBytes, 0, result, 0, ExtensionBytes.Length);
              return result;
          }
          set
          {
              byte[] result = new byte[value.Length * sizeof(float)];
              Buffer.BlockCopy(value, 0, result, 0, result.Length);
              ExtensionBytes = result;
          }
      }
  }
  </code></pre>
  
  <p>It works but seems very wasteful, in C I would probably use something like</p>
  
  <pre><code>(float*)&amp;byte_array)
  </code></pre>
  
  <p>Which seems a neater solution that means data isn't being copied back and forth in memory all the time.</p>
  
  <p>Is there a similar neat solution in c#</p>
  
  <p>Alternatively a way to store a float or double array in entity framework.</p>