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