【发布时间】:2012-08-10 17:03:46
【问题描述】:
我认为这是一种很好的做法,当使用Assembly.GetManifestResourceStream 方法访问嵌入式程序集资源时,在完成后关闭返回的Stream。但是,我刚刚在以下文章中发现了一些内容:
http://msdn.microsoft.com/en-us/library/ms950960.aspx
// Get the stream that holds the resource
// NOTE1: Make sure not to close this stream!
// NOTE2: Also be very careful to match the case
// on the resource name itself
Stream stream =
assem.GetManifestResourceStream("Azul.jpg");
// Load the bitmap from the stream
this.BackgroundImage = new Bitmap(stream);
这里的评论说应该不关闭流,尽管文章没有提及原因。谷歌搜索没有提供任何结论;有些人似乎关闭了这个流,有些人没有,并说垃圾收集器会处理它。
我应该关闭Assembly.GetManifestResourceStream 返回的流吗?我不应该有什么特别的原因吗?
【问题讨论】: