简体   繁体   中英

Does Grid.Children.Clear() remove controls from memory?

I create controls from code and add them as children to a grid, and next I clear them, eg

Button myButton = new Button();
myGrid.Children.Add(myButton);
myGrid.Children.Clear();

Will the controls (the button in this example) be removed from memory or do I have to explicitly remove them?

They will be eligible for garbage collection and eventually be removed from memory by the CLR/garbage collector without you having to do anything assuming that you don't have any other references to the controls somewhere in your application (which you don't if you simply create a control, add it to a Panel and then remove it).

Automatic memory management is one of the services that the CLR provides during the managed execution of a .NET/WPF application. You can read more about this on MSDN: https://msdn.microsoft.com/en-us/library/f144e03t(v=vs.110).aspx .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM