Who is finalize and dispose used?

Source: Internet
Author: User
From http://www3.ccw.com.cn/club/essence/200201/7841.htm
Explicit calling of finalizer in C # is not allowed. It can only be called by the fragment collection program. If you want to release a limited number of non-manageability resources (such as file handles) that are no longer in use as soon as possible, you should use the idisposable interface, which has a dispose method, it can help you complete this task. Dispose is a method that can release non-manageability resources without waiting for finalize to be called.

If the dispose method is used, the fragment collection program should be prevented from executing the Finalize method on the corresponding object. Therefore, you need to call the Static Method GC. suppressfinalize and pass the pointer of the corresponding object to it as a parameter. The Finalize method can call the dispose method. Accordingly, we can get the following code:

Public void dispose ()
{
// Complete the cleanup operation

// Notify GC not to call the Finalize method again
GC. suppressfinalize (this );
}

Public override void finalize ()
{
Dispose ();
Base. Finalize ();
}

For some objects, it is more appropriate to call the close method (for example, it is more appropriate to call close for object objects than dispose ), you can create a dispose method for the private attribute and a close method for the public attribute, and call dispose to call the close method for some objects.

Dispose is always called, and the execution of finalizer is also uncertain (we cannot control when the GC will run ), C # provides a using statement to ensure that the dispose method is called as early as possible. The general method is to define which object to use, and then use parentheses to specify an activity range for these objects. In the case of the innermost parentheses, the dispose method will be automatically called, process the object.

Using system. drawing;
Class Tester
{
Public static void main ()
{
Using (font thefont = new font ("Arial", 10.0f ))
{
// Use thefont object

} // The compiler will call dispose to process thefont objects

Font anotherfont = new font ("Courier", 12.0f );

Using (anotherfont)
{
// Use the anotherfont object

} // The compiler will call dispose to process the anotherfont object

}

}

In the first part of this example, the font object is created in the using statement. When the using statement ends, the system calls dispose to process the font object. In the second part of this example, the font object is created outside the using statement. When you decide to use it, place it in the using statement. When the using statement ends, the system will call dispose.

The Using statement can also prevent other accidents and ensure that the system will call dispose.

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.