C # in-depth analysis (2) -- destructor

Source: Internet
Author: User
Article 2 destructor

The Destructor are rarely used in C #, so many people have forgotten it. Although it is of little use, it is useless to study it.

I. Features of destructor:

Destructor can only exist in classes, but not in structures. destructor cannot have any modifiers, including access control modifiers, static modifiers, and abstract modifiers, virtual modifiers and so on are not available; destructor have no parameters, which means there cannot be any overload.

Ii. Time for calling the Destructor:

The Destructor will be called when the object is recycled by the garbage collector, but the Garbage Collector has a feature that it is lazy,
It does not recycle variables immediately after they are out of scope and life, but is recycled only when they are considered appropriate, generally when memory is tight. For example:

C # code
   Class mainform: FORM {button BTN = new button (); Public mainform () {BTN. click + = btn_click;} void btn_click (Object sender, eventargs e) {demo de = new demo () ;}} class demo {~ Demo () {MessageBox. Show ("destructor called ");}}

After the method btn_click is returned, de should be terminated, but its destructor is not called, indicating that the garbage collector has not recycled it; when you close the form, the Destructor is executed, which means that at the end of the program, the garbage collector is reluctant to recycle it (^-^ ).
Of course, we can call GC. Collect () to forcibly recycle:

C # code
   Class mainform: FORM {button btn1 = new button (); button btn2 = new button (); Public mainform () {btn1.click + = btn1_click; btn2.click + = btn2_click; btn2.top = 40; this. controls. add (btn1); this. controls. add (btn2);} void btn1_click (Object sender, eventargs e) {demo de1 = new demo (); de1 = NULL; demo de2 = new demo (); new demo (); GC. collect ();} void btn2_click (Object sender, eventargs e) {GC. c Ollect () ;}} class demo {~ Demo () {MessageBox. Show ("destructor called ");}}

When you click btn1, de1 and new demo () are terminated and destructor are called.
De2 has not yet had a life cycle, so although GC is called. the collect method will not be recycled. When btn1_click is returned, de2 has a life cycle, but due to the laziness of the garbage collector, it is still not recycled until you click btn2 to call GC. for the collect method, de2 is recycled and Its destructor is called.

One way to prevent the calling of the Destructor is to implement the idisposable interface, which defines a unique method: dispose ().
This can block destructor because GC is usually called internally. suppressfinalize () method. In other words, if you are bored, you can implement this interface without calling GC. suppressfinalize method (^-^), which makes no sense, because the Destructor cannot be blocked:

C # code
          void btn1_Click(object sender, EventArgs e)        {            Demo de1 = new Demo();            Demo de2 = new Demo();            GC.SuppressFinalize(de1);            de1 = null;            GC.Collect();        }

Now de1's destructor will not be called.

Iii. essence of destructor:

An destructor is essentially a method in the following form:

C # code
           protected void Finalize()        {        }

We usually think that the Destructor can only be called by the system, but not by the programmer. In fact, this is not completely correct. The Destructor can also be explicitly called, after all, it is just a method:

C # code
   Demo de = new Demo();typeof(Demo).GetMethod("Finalize",BindingFlags.Instance|BindingFlags.NonPublic).Invoke(de, null);

Related Article

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.