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(); } }
{ x = e; try { s } finally { x.Dispose(); } }
是的, 使用创建一个try..finally块 ,所以它处理ms(甚至在你将ns设置为null的情况下进行空检查)。
Yes, the whole idea behind the Using statement is that it automatically disposes of whatever stream/object you are "using". nicely done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.