There are three ways to destroy objects in C # FINALIZE,DISPOSE,GC.

Source: Internet
Author: User

1. The Finalize method (in C # is a destructor, the following destructor) is used to dispose of unmanaged resources, and managed resources are automatically reclaimed by the GC. So, we can also differentiate between managed and unmanaged resources. All of the resources that will be automatically reclaimed by GC are managed resources, and resources that cannot be automatically reclaimed by GC are unmanaged resources. The use of unmanaged resources directly in our classes is rare, so we do not have to write destructors basically.

2, most of the unmanaged resources will have a lot of negative impact on the system, such as the database connection is not released may cause the connection pool to run out of available database connections. A file that does not close causes other processes to not read and write to the file.

Implementation Model:
1. Because most unmanaged resources require manual release, we should expose a method specifically for releasing unmanaged resources. The Dispose method that implements the IDispose interface is the best model because C # supports the using statement fast and can automatically call the Dispose method when it leaves the statement block.

2, although you can manually release unmanaged resources, we still want to release the unmanaged resources in the destructor, so that is a secure application. Otherwise, if you forget to manually release unmanaged resources because of a programmer's negligence, it can have disastrous consequences. So releasing an unmanaged resource in a destructor is a remedial measure, at least for most classes.

3, because the invocation of destructors will cause the GC to reduce the efficiency of object recycling, so if you have done what the destructor is doing (for example, releasing unmanaged resources), you should use the SuppressFinalize method to tell the GC not to need to execute an object's destructor.

4. Destructors can only release unmanaged resources and cannot operate on any managed objects/resources. Because you can't predict the timing of the destructor, you might have freed the managed resources you were working on when the destructor was executed. This will result in serious consequences.

5. (This is a rule) if a class has a member that implements the IDispose interface type and creates (note that it is created, not received, it must be created by the class itself) its instance object, then this class should also implement the IDispose interface, And in the Dispose method, all the dispose methods that implement the members of the IDispose interface are called.
This is the only way to ensure that all objects that implement the IDispose interface's object's Dispose method can be called to ensure that any resources that need to be freed can be released manually.

There are three ways to destroy objects in C # FINALIZE,DISPOSE,GC.

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.