title: What is the difference between calling Stream.Write and using a StreamWriter?
body: <p>What is the difference between instantiating a <code>Stream</code> object, such as <code>MemoryStream</code> and calling the <code>memoryStream.Write()</code> method to write to the stream, and instantiating a <code>StreamWriter</code> object with the stream and calling <code>streamWriter.Write()</code>?</p>
  
  <p>Consider the following scenario:</p>
  
  <p>You have a method that takes a Stream, writes a value, and returns it.  The stream is read from later on, so the position must be reset.  There are two possible ways of doing it (both seem to work).</p>
  
  <pre><code>// Instantiate a MemoryStream somewhere
  //     - Passed to the following two methods
  MemoryStream memoryStream = new MemoryStream();
  
  // Not using a StreamWriter
  private static Stream WriteToStream(Stream stream, string value)
  {
      stream.Write(Encoding.Default.GetBytes(value), 0, value.Length);
      stream.Flush();
      stream.Position = 0;
      return stream;
  }
  
  // Using a StreamWriter
  private static Stream WriteToStreamWithWriter(Stream stream, string value)
  {
      StreamWriter sw = new StreamWriter(stream);
      sw.Write(value, 0, value.Length);
      sw.Flush();
      stream.Position = 0;
      return stream;
  }
  </code></pre>
  
  <p>This is partially a scope problem, as I don't want to close the stream after writing to it since it will be read from later.  I also certainly don't want to dispose it either, because that will close my stream.  The difference seems to be that not using a StreamWriter introduces a direct dependency on Encoding.Default, but I'm not sure that's a very big deal.  What's the difference, if any?</p>