title: Deserializing a large block blob using MemoryStream
body: <p>I need to deserialize large files downloaded from Azure Blob Storage. </p>
  
  <p><strong>Would using a <code>MemoryStream</code> in this fashion be the most optimal way to deserialize the file into an <code>object</code>?</strong>  </p>
  
  <p>My main concern here is RAM usage.</p>
  
  <pre><code>BinaryFormatter formatter = new BinaryFormatter();
  
  using (MemoryStream stream = new MemoryStream())
  {
      blob.DownloadToStream(stream);
      stream.Position = 0;
      return formatter.Deserialize(stream);
  }
  </code></pre>
  
  <p>Another option would be to download the entire file as a byte array. Note that this files could be up to 50mb in size.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Further from Guillaume's answer:</p>
  
  <p>Would then this then be more optimal when serializing and uploading?</p>
  
  <pre><code>BinaryFormatter formatter = new BinaryFormatter();
  using (var stream = blob.OpenWrite())
  {
      formatter.Serialize(stream, value);
      blob.UploadFromStream(stream);
  }
  </code></pre>