Dispose on nested Disposable items?

后端 未结 3 463
礼貌的吻别
礼貌的吻别 2021-01-18 21:21

I wanted to know if there are any conventions regarding disposal of disposable items nested inside another disposable item(in a property/public field, not as private members

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 22:09

    The rule of thumb I normally follow is that the class that creates a disposable object, will also dispose it. As an example: a SqlCommand does not dispose its connection, because it didn't create it. The StreamReader has a strange behavior in this sense, because it will always dispose the underlying stream, even if it is supplied from the outside (I find this very annoying, please vote HERE when you like Microsoft to fix this).

提交回复
热议问题