I am currently trying to implement my first dispose on some of my objects, and was wondering if its a bad idea to go through all the properties per reflection and just set them to null?
2 Answers
Implementing IDisposable
doesn't mean that you've to set all the fields to null
. GC will take care of that when they are not reachable from the root objects.
And setting null
will not do anything still the object will be in memory till GC notices it is not having managed any references.
IMO you can't see a comprehensive answer more than this Proper use of the IDisposable interface
Setting null helps only when there is only one managed reference left, you don't need it though but you need to keep the encapsulating type alive then you'll set reference to null
. Otherwise if encapsulating type itself is not reachable then setting null
doesn't makes any sense.

- 1
- 1

- 72,067
- 7
- 111
- 189
Yes it is a bad idea.
First of all, some of your properties may be classes that implement the IDisposable
interface, so you'd have to call the Dispose
method first on them.
Then if one of your properties is a class that depends on COM components or uses some, you have to dispose it correctly or you will end up with memory leaks

- 3,077
- 2
- 15
- 27