一个用包裹的MemoryStream包裹、MemoryStream

2023-09-03 00:27:34 作者:骑着小猪去兜风

我已经告诉System.IO.MemoryStream不需要包装在使用的块,因为没有基本的资源,这有点违背我一直被告知流( 如果有疑问,可使用的使用的)。

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

 使用(MemoryStream的memStream =新的MemoryStream(100))
{
            //做的东西
}
 

解决方案

C#的成语是:如果一个对象实现的IDisposable 然后你使用使用块。这将允许正在使用的对象的所有资源被妥善处理。你不是想知道的MemoryStream 的实施细节。什么,你所知道的是,它实现了的IDisposable ,所以你应该处置得当。另外,你认为你现在知道,它并不需要释放任何资源,但你怎么知道,在未来的的MemoryStream 不会改变其基本的实施,使它确实需要使用释放资源使用处置?你不这样做,那么因为它实现 IDispoable 你有更多的面向未来的code,只需使用模式了。第二,如果你改变了code在未来使用的不同类型的,它有管理的资源?通过包装的MemoryStream 使用块,现在可以减少日后的维护问题;再次,这是使用对象的正确方法,特别是 s表示实施的IDisposable 。第三,它的信号,以飨读者,您使用对象完成一个清晰的方式;这就是你的code读者会希望看到,当他们看到你正在使用的实施的IDisposable 的对象。最后,这是目前实施 MemoryStream.Dispose

 保护覆盖无效的Dispose(BOOL处置){
    尝试 {
        如果(处置){
            this._isOpen = FALSE;
            this._writable = FALSE;
            this._expandable = FALSE;
        }
     }
     最后 {
         base.Dispose(处置);
     }
}
 

那么,这标志着流作为封闭的,没有可写和不可增长。如果莫名其妙地有人得到了保持的参照同的MemoryStream ,现在变得不可用他们这可能是一件好事。但是,这真的是最重要的问题;实现细节并不重要。

建议103 区分组合和继承的应用场合

使用一个使用块,因为的MemoryStream 实施 IDispoable 。不要不要使用使用块,因为你认为的MemoryStream 没有需要被释放的任何资源

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").

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

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

解决方案

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);
     }
}

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.

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.