Why Dispose is not called even with using-statement?

As per MS Docs: try-finally (C# Reference) Within a handled exception, the associated finally block is guaranteed to be run. However, if the exception is unhandled, execution of the finally block is dependent on how the exception unwind operation is triggered. That, in turn, is dependent on how your computer is set up. As you … Read more

What is IDisposable for?

Garbage collection is for memory. You need to dispose of non-memory resources – file handles, sockets, GDI+ handles, database connections etc. That’s typically what underlies an IDisposable type, although the actual handle can be quite a long way down a chain of references. For example, you might Dispose an XmlWriter which disposes a StreamWriter it … Read more

How to dispose a class in .net?

IDisposable has nothing to do with freeing memory. IDisposable is a pattern for freeing unmanaged resources — and memory is quite definitely a managed resource. The links pointing to GC.Collect() are the correct answer, though use of this function is generally discouraged by the Microsoft .NET documentation. Edit: Having earned a substantial amount of karma … Read more

Clear controls does not dispose them – what is the risk?

Asking for modifications like this is pointless, the Windows Forms team has been disbanded quite a while ago. It is in maintenance mode, only security issues and OS incompatibilities are considered. It is otherwise simple enough to create your own method to do this: public static class ExtensionMethods { public static void Clear(this Control.ControlCollection controls, … Read more