title: MemoryStream.ToArray() return empty array if StreamWriter is not disposed
body: <p>I'm trying to get array of bytes from my model to put it in the file. I have a method as such:</p>
  <pre><code>        public static byte[] GetByteArray(List&lt;MyModel&gt; models)
      {
          using var ms = new MemoryStream();
          using var sw = new StreamWriter(ms);
  
          foreach (var model in models)
          {
              sw.Write(model.Id + &quot;,&quot; + model.Name);
              sw.WriteLine();
          }
  
          sw.Dispose();
          return ms.ToArray();
      }
  </code></pre>
  <p>This method works fine, but as may think I don't need to dispose <em>StreamWriter</em> manually, cause I have a using statement. I thought as well, but when I remove <code>sw.Dispose();</code> the <code>ms.ToArray();</code> returns an empty array. Can someone explain this behavior to me?</p>