问题描述
作为的MemoryStream是一个非托管资源它总是要处理?
As MemoryStream is an unmanaged resource does it always have to be disposed?
由于:
1) A method is invoked.
2) A MemoryStream object is created (MemoryStream ms = new MemoryStream();).
3) An exception occurs and is caught from the invoking classes.
将MemoryStream对象的引用,因此丢失。请问这种情况需要一个try /最后块(或使用语句)?
The reference on the MemoryStream object is therefore lost. Does this scenario need a try/finally-block (or using-statement)?
推荐答案
在一般情况下,所有可支配的对象必须进行处理。
In general, all disposable objects must always be disposed.
不过,的MemoryStream
实际上并不需要处理,因为它没有任何非托管资源。 (这只是一个字节[]
和 INT
)
它是一次性摆在首位的唯一原因是,它继承了抽象的流
类,它实现的IDisposable
。
However, MemoryStream
doesn't actually need to be disposed, since it doesn't have any unmanaged resources. (It's just a byte[]
and an int
)
The only reason it's disposable in the first place is that it inherits the abstract Stream
class, which implements IDisposable
.
注意的每次的其他流必须进行处理。
Note that every other stream must be disposed.
这篇关于MemoryStream的,必须明确地处置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!