title: Serializing/deserializing with memory stream
body: <p>I'm having an issue with serializing using memory stream. Here is my code:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// serializes the given object into memory stream
  /// &lt;/summary&gt;
  /// &lt;param name="objectType"&gt;the object to be serialized&lt;/param&gt;
  /// &lt;returns&gt;The serialized object as memory stream&lt;/returns&gt;
  public static MemoryStream SerializeToStream(object objectType)
  {
      MemoryStream stream = new MemoryStream();
      IFormatter formatter = new BinaryFormatter();
      formatter.Serialize(stream, objectType);
      return stream;
  }
  
  /// &lt;summary&gt;
  /// deserializes as an object
  /// &lt;/summary&gt;
  /// &lt;param name="stream"&gt;the stream to deserialize&lt;/param&gt;
  /// &lt;returns&gt;the deserialized object&lt;/returns&gt;
  public static object DeserializeFromStream(MemoryStream stream)
  {
      IFormatter formatter = new BinaryFormatter();
      stream.Seek(0, SeekOrigin.Begin);
      object objectType = formatter.Deserialize(stream);
      return objectType;
  } 
  </code></pre>
  
  <p>The error I'm getting is as follow:
  stream is not a valid binary format. The starting contents (in bytes) are: blah....</p>
  
  <p>I'm not exactly sure what is causing the error. Any help would be greatly appreciated.</p>
  
  <p>Example of the call:</p>
  
  <pre><code>Dog myDog = new Dog();
  myDog.Name= "Foo";
  myDog.Color = DogColor.Brown;
  
  MemoryStream stream = SerializeToStream(myDog)
  
  Dog newDog = (Dog)DeserializeFromStream(stream);
  </code></pre>