将 MemoryStream 包装在 using 中

wrapping MemoryStream in a using(将 MemoryStream 包装在 using 中)
本文介绍了将 MemoryStream 包装在 using 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

有人告诉我 System.IO.MemoryStream 不需要包含在 using 块中,因为没有底层资源,这有点违背我一直被告知的关于流的内容(如果有疑问,请使用 using").

I've been told that System.IO.MemoryStream need not be wrapped in a using block because there is no underlying resource, this kinda goes against what i've always been told about streams ("if in doubt, use a using").

这是真的吗?那么为什么 MSDN 示例 使用一个(总结如下)?

Is this true? Why then does MSDN example use one (summarized below)?

using(MemoryStream memStream = new MemoryStream(100))
{
            // do stuff
}

推荐答案

C# 的习惯用法是,如果一个对象实现了 IDisposable,那么你使用 using 块.这将允许正确处理对象使用的所有资源.您不应该知道 MemoryStream 的实现细节.您所知道的是它实现了 IDisposable,因此您应该正确处理它.此外,您认为您现在知道它不需要释放任何资源,但是您怎么知道将来 MemoryStream 不会更改其底层实现,以便它确实使用需要的资源?使用 Dispose 释放?您没有,因此,由于它实现了 IDispoable,您现在只需使用该模式即可获得更多面向未来的代码.其次,如果您将来更改代码以使用具有托管资源的不同类型的 Stream 会怎样?通过将 MemoryStream 包装在 using 块中,您可以减少将来的维护问题;同样,这是使用对象的正确方法,特别是实现 IDisposableStreams.第三,这是向读者发出信号,表明您已完成使用该对象的一种明确方式;这是您的代码的读者在看到您使用实现 IDisposable 的对象时所期望看到的.最后,这是 MemoryStream.Dispose 的当前实现:

The C# idiom is that if an object implements IDisposable then you use a using block. This will allow all resources being used by the object be disposed of properly. You're not suppose to know the implementation details of MemoryStream. What you do know is that it implements IDisposable so you should dispose of it properly. Further, you think that you know now that it doesn't need to free any resources but how do you know that in the future MemoryStream won't change its underlying implementation so that it does use resources that need to be freed using Dispose? You don't, so since it implements IDispoable you have more future-proof code by just using the pattern now. Second, what if you change your code in the future to use a different type of Stream that does have managed resources? By wrapping the MemoryStream in a using block now you reduce maintenance issues in the future; again, this is the correct way to use objects, and specifically Streams that implement IDisposable. Third, it's a clear way to signal to readers that you are done using the object; it's what readers of your code will expect to see when they see you are using objects that implement IDisposable. Finally, this is the current implementation of MemoryStream.Dispose:

protected override void Dispose(bool disposing) {
    try {
        if (disposing) {
            this._isOpen = false;
            this._writable = false;
            this._expandable = false;
        }
     }
     finally {
         base.Dispose(disposing);
     }
}

因此,这将流标记为关闭、不可写和不可增长.如果其他人以某种方式获得了对同一个 MemoryStream 的引用,那么他们现在就无法使用它,这可能是一件好事.但这确实是最不重要的问题;实现细节无关紧要.

So, this marks the stream as closed, not-writable and not-growable. If somehow someone else got a hold of a reference to the same MemoryStream, it now becomes unusable to them which could be a good thing. But this is really the least important issue; the implementation details don't matter.

使用 using 块,因为 MemoryStream 实现了 IDispoable.不要不使用 using 块,因为您认为 MemoryStream 没有任何需要释放的资源.

Use a using block because MemoryStream implement IDispoable. Don't not use a using block because you think that MemoryStream doesn't have any resources that need to be freed.

这篇关于将 MemoryStream 包装在 using 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?(Linq to SQL 中是否提供 Unpivot(非 Pivot)功能?如何?)
How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)