我应该关闭Assembly.GetManifestResourceStream返回的流吗?

时间:2012-08-10 17:03:47

标签: c# stream

我原以为使用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返回的流吗?我不应该有特殊原因吗?

1 个答案:

答案 0 :(得分:4)

该评论不希望您关闭它,因为它继续从中创建Bitmap对象。 通常,您应该在使用完流后关闭流,否则您的应用程序将受到内存泄漏的影响。

相关问题