Should “Dispose” only be used for types containing unmanaged resources?

后端 未结 15 1665
执笔经年
执笔经年 2020-12-05 01:41

I was having a discussion with a colleague recently about the value of Dispose and types that implement IDisposable.

I think there is value

15条回答
  •  臣服心动
    2020-12-05 02:10

    Short Answer: Absolutely NOT. If your type has members that are managed or unmanaged, you should implement IDisposable.

    Now details: I've answered this question and provided much more detail on the internals of memory management and the GC on questions here on StackOverflow. Here are just a few:

    • Is it bad practice to depend on the .NET automated garbage collector?
    • What happens if I don't call Dispose on the pen object?
    • Dispose, when is it called?

    As far as best practices on the implementation of IDisposable, please refer to my blog post:

    How do you properly implement the IDisposable pattern?

提交回复
热议问题