MemoryStream in Using Statement - Do I need to call close()

萝らか妹 提交于 2019-11-29 09:03:35

No, it's not.

using ensures that Dispose() will be called, which in turn calls the Close() method.

You can assume that all kinds of Streams are getting closed by the using statement.

From MSDN:

When you use an object that accesses unmanaged resources, such as a StreamWriter, a good practice is to create the instance with a using statement. The using statement automatically closes the stream and calls Dispose on the object when the code that is using it has completed.

When using a memory stream in a using statement do I need to call close?

No, you don't need. It will be called by the .Dispose() method which is automatically called:

using (MemoryStream ms = new MemoryStream(byteArray)) 
{  
    // stuff 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!