title: C# flushing StreamWriter and a MemoryStream
body: <p>I use the following snippet of code, and I'm unsure whether I need to call the <code>Flush</code> methods (once on <code>StreamWriter</code>, once on <code>MemoryStream</code>):</p>
  
  <pre><code>    //converts an xsd object to the corresponding xml string, using the UTF8 encoding
      public string Serialize(T t)
      {
          using (var memoryStream = new MemoryStream())
          {
              var encoding = new UTF8Encoding(false);
  
              using (var writer = new StreamWriter(memoryStream, encoding))
              {
                  var serializer = new XmlSerializer(typeof (T));
                  serializer.Serialize(writer, t);
                  writer.Flush();
              }
  
              memoryStream.Flush();
  
              return encoding.GetString(memoryStream.ToArray());
          }
      }
  </code></pre>
  
  <p>First of all, because the code is inside the <code>using</code> block, I think the automatically called dispose method might do this for me. Is this true, or is flushing an entirely different concept? </p>
  
  <p>According to stackoverflow itself: </p>
  
  <blockquote>
    <p><em>Flush meaning clears all buffers for a stream and causes any buffered data to be written to the underlying device.</em> </p>
  </blockquote>
  
  <p>What does that mean in the context of the code above?</p>
  
  <p>Secondly, the flush method of the <code>MemoryStream</code> <a href="http://msdn.microsoft.com/en-us/library/system.io.memorystream.flush%28v=vs.110%29.aspx" rel="noreferrer">does nothing according to the api</a>, so what's up with that? why do we call a method that does nothing?</p>