I want to dispose all resources (binded events, variables, objects, data structures, data bindings, etc.) of my form on form close. Currently I,m disposing my resources explicitly one by one on one of my form just to test this operation. But my application is big and there are many forms in it and if i started disposing all resources explicitly on every form, it will take time. So my question is that is there a general method or technique? so that all my resources gets disposed rather then doing it explicitly one by one.
Thanks.
You should use using statements for your IDisposable objects, rather than calling Dispose().
As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.
using (Font font1 = new Font("Arial", 10.0f))
{
byte charset = font1.GdiCharSet;
}
https://msdn.microsoft.com/en-us/library/yh598w02.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With