Learning: differences between close and dispose in C #

Source: Internet
Author: User
In C #, The Destructor are called by the garbage collector. When the referenced data on the stack is invalid, it is called implicitly. Generally, it is called when the referenced variable is out of its scope.

The close and dispose functions are a convention:
After the close function is called to release resources, it may need to be used again, but the resources released by the dispose function are no longer used;
In addition, if the class implements the idisposable interface and you can use the Using Keyword when instantiating the class, the dispose function is called implicitly when it exceeds the scope of the using keyword,
For example:

Using (ctest inst = new ctest ()) Itpub personal space f1o) u3bc) E

{

// Do something;

} // Call dispose method here

 

Differences:

1. Add a MessageBox in the protected override void dispose (bool disposing) method, and then add a button in the form.CodeIs this. Close ();

Messabebox will not pop up, that is, the dispose method will not be called;

 

Change the button Event code to: This. Dispose ();Itpub personal space "} 4yns-y6pw! HD | S

MessageBox will pop up, that is, directly destroy all resources opened by the form.

3fj! C ^ lcx0

2. Use

Form. frm2 = new form2 ();Itpub personal space C' "y6k $ J _

Frm2.showdialog ();

 

After you use the close () method in form2 to close the form form2, you can still directly use frm2.showdialog () in form1 to display the form2.
Wwy7iyp % Q % @ 0

After the form form2 is closed using the dispose () method, you cannot directly use frm2.showdialog () in form1 to display form2. You still need to create a new instance form. frm2 = new form2 ();

ArticleSource: http://space.itpub.net/12639172/viewspace-580649

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.