Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

c# - Does Form.Dispose() call controls inside's Dispose()?

When I create a Form, the auto-generated code doesn't include an overrided Dispose method. Does that mean Dispose is not being called for all the controls in the form?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you call Dispose on the form, it will call Dispose for each control in its Controls collection. Those controls will in turn do the same, so in the end all controls' Dispose method should have been invoked. Note that this is not based on whether the controls are present in the designer or not; it is based on what control instances that are found in the Controls collection of the form at the time the call to Dispose is done.

The only case when I could see that this would not happen is if you create some container control yourself and override Dispose without propagating the call either to the base class or iterate over the contained controls and call Dispose on them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...