Remove (), Clear () or Dispose ()

Simple question...

I have controls that the user can move around my form at runtime. And they can also remove them. Should I just call .Dispose(); when they click the "Delete" button, or I need to do something like panel1.Controls.Clear(Control); ?... Or something else?

Thanks:)

Bael

+4
source share
2 answers

You must remove it from the Controls parent collection, as described in Darin Dimitrovโ€™s answer, and also call Dispose:

 panel.Controls.Remove(someControlInstance); someControlInstance.Dispose(); 

You should always call Dispose on objects that implement IDisposable when you are done with them, so that any unmanaged resources that they own are freed immediately.

+5
source

Just remove the control from the panel:

 panel.Controls.Remove(someControlInstance); 

As soon as there are no more links to it, it will be garbage collected and unmanaged resources will be correctly allocated.

+2
source

Source: https://habr.com/ru/post/1300507/


All Articles