MemoryStream must be explicitely be disposed?

后端 未结 4 2101
别跟我提以往
别跟我提以往 2020-12-19 07:20

As MemoryStream is an unmanaged resource does it always have to be disposed?

Given:

1) A method is invoked.
2) A MemoryStream object is created (Mem         


        
4条回答
  •  渐次进展
    2020-12-19 08:01

    In general, all disposable objects must always be disposed.

    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.

提交回复
热议问题