Question about cleaning up resources


I have read that this.Close(); does not only closes the form but cleans
up the variable in memory also. So I have put the line when the user closes the
main form. But what about other forms. F.e I have a second form called Settings.
That can be called by mainmenu-item Settings it will then execute the following code:

        private void mmSettings_Click(object sender, EventArgs e)

and it has a close-button:

        private void btnOK_Click(object sender, EventArgs e)
            this.DialogResult = DialogResult.OK;

But doesn't this close the settingsform and not clean up the form in memory?
How can I close the form and clean up the form in memory?
And is the keyword 'Dispose' for cleaning up objects?

Peter KiersOperatorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
     private void btnOK_Click(object sender, EventArgs e)
            this.DialogResult = DialogResult.OK;  //Signal what the return of the form will be WHEN closed

this.Close();  //Now close the form

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
Dispose should be called automatically for a form - you should not need to call it explicitly.

The memory is only 'cleaned' when the variable goes out of scope (and the garbage collector runs).

eg. some air code

MyForm frm =new MyForm();  //assign memory for the frm variable
frm.ShowDialog();  //Display the dialog as modal
int x = frm.SomeIntVariable;  //frm still exists (note NOT displayed) and is valid
int y = frm.SomeOtherInt;  //frm is now out off scope and invalid - error if you attempt to use it
Peter KiersOperatorAuthor Commented:

Asim NazirCommented:
If you are sure that you are not going to referenc this form after closing it then best way is to dispose it instead of closing it. else If you want to access some properties of the form once form is closed then you can close it. Once closed, you can access any properties defined in the form and then dispose it.

This is the best implementation.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.