I have recently taken over the responsibility for a code-base written using the .NET compact framework for mobile devices and I see the code scattered with Dispose() calls all over the place.
This seems to be on
- ADO.NET db commands
- Windows Forms
I thought that the Garbage collector would handle memory management. I know they might hang in memory a little longer without Dispose(), but is there much advantage or gain to having these calls? It seems to pollute the neatness of the code.
It really depends a lot on what is being Disposed by these calls.
IDisposableisn’t (necessarily) about releasing memory, but rather about releasing resources. In general, you always should call Dispose on objects when it’s possible to do so without huge hoops in your logic, and not rely upon the GC to cleanup resources unless necessary.Given that you’re running on CF, it’s likely even more critical. If your program is targeting a platform with limited resources, cleaning up those resources as soon as possible is very important.