The Dispose() methodThe Dispose method performs all object cleanup, so the garbage collector no longer needs to call the objects' Object. Finalize override. Therefore, the call to the SuppressFinalize method prevents the garbage collector from running the finalizer. If the type has no finalizer, the call to GC.
In the context of C#, dispose is an object method invoked to execute code required for memory cleanup and release and reset unmanaged resources, such as file handles and database connections.
Dispose() will not be called automatically. If there is a finalizer it will be called automatically. Implementing IDisposable provides a way for users of your class to release resources early, instead of waiting for the garbage collector.
The Dispose method is automatically called when a using statement is used. All the objects that can implement the IDisposable interface can implement the using statement. You can use the ildasm.exe tool to check how the Dispose method is called internally when you use a using statement.
All Component
classes implement a Disposed
event. You can add an event handler for that event and clean up things in there.
For example, in your UserControl
you could add following method:
private void OnDispose(object sender, EventArgs e)
{
// do stuff on dispose
}
And in constructor (or in Load
event handler) add the following line:
Disposed += OnDispose;
In such a case I move the generated Dispose
method to the main file and extend it. Visual Studio respects this.
An other approach would be using a partial method (C# 3.0).
I believe in this case the code-generator honors your code. It should be safe to put it in the codebehind.
In VS 2005 (and 2008) you can update the Dispose
method and it will not get removed when you edit the control from the designer.
You can move it out from the .designer.cs file and into the main .cs file if you want. As has been said already, it won't be overwritten.
You just need to overload the public void Dispose() method in the Component Class that the user control inherits.
make sure you pass the call to the base method as well as doing your dispose functionally or you'll break the functionality unless you fully implement it
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