Tag: dispose

Avoid C# Memory Leaks with Destructor and Dispose

Here are two classes that allocate resources, the first class needs to implement a destructor and Dispose, but the second class doesn’t need to. The difference is that the first class allocates resources in the constructor and doesn’t clean them up immediately. In contrast, the second class allocates resources in a member function and carefully frees them before exiting (using a finally block), so the second class author doesn’t need to bother writing Dispose and a destructor.

Back To Top