Suppose I have a method like so:
public byte[] GetThoseBytes()
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
ms.WriteByte(1);
ms.WriteByte(2);
return ms.ToArray();
}
}
Would this still dispose the 'ms' object? I'm having doubts, maybe because something is returned before the statement block is finished.
Thanks, AJ.
Yes. using (x = e) { s }
is sugar for { x = e; try { s } finally { x.Dispose(); } }
Yes, Using creates a try..finally block, so it disposes the ms (and even does a null check in case you set ns to null).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With