title: Writing arbitrary type instances into a MemoryStream in C#
body: <p>In Delphi, one can do the following:</p>
  
  <pre><code>var
    ms  : TMemoryStream;
    i   : Integer;
  begin
    ms := TMemoryStream.Create;
    i := 1024;
    ms.Write(@i, SizeOf(Integer));
    ms.Free;
  end;
  </code></pre>
  
  <p>This will write the memory contents of i into ms.</p>
  
  <p>The .Net version of MemoryStream doesn't have such a feature (neither the managed nor unmanaged versions). I know .Net doesn't work on the same principles as Delphi in this regard.</p>
  
  <p>How does one do this in C#? I am interested in the "best practice" and the fastest methods.</p>