. Net confirm destruction

Source: Internet
Author: User

In. Net, the garbage collector is responsible for recycling the reference type objects you have created, but the collection time cannot be accurately estimated, so this is called uncertain destruction. The value type is automatically released, so it is not discussed in this article.
However, some scarce resources, such as file handles and database connections, need to be released as soon as possible. How can this problem be achieved. The simplest way is to call GC. Collect () to force the Garbage Collector to work. However, this method will reduce the performance unless you have.
Is there a better way?
"Destructor"
Differences between "destructor" and c ++ destructor
What about "destructor? For example:
Class Class1
{
Public Class1 ()
{
Console. WriteLine ("constructor ");
}
 
~ Class1 ()
{
Console. WriteLine ("destructor ");
}
}
In fact, the "destructor" in c # is not the destructor in the minds of c ++ programmers. In c ++, when jumping out of the stack where the object exists or calling delete, the Destructor will be called. C # provides a similar syntax, but the semantics is completely different. In fact, this function only reloads the Finalize function of the System. Object Class.
 
System. Object is the base type of all types in c #. Because it is an implicit derivation, you do not need to specify it in the derived list. The access permission of the Finalize function is protected and can only be used by itself and the derived class. In fact, when the Garbage Collector destroys an object, it will give the object a chance to call Finalize, in this way, we can put the logic we need in the "destructor. If an exception occurs during object construction, this method is also called.
However, after the above analysis, we can see that the "destructor" Cannot Let us determine to destroy an object.
 
Hazards of "destructor"
By the way, C # code should always use the "destructor" instead of manually reload the Finalize function, because the "destructor" will automatically put the internal code in the try block, in addition, the base is called within the finnally block. finalize method.
Normally, the "destructor" is not recommended because it will increase the age of the class and other referenced classes, which will lead to the early running of the garbage collector, this affects the performance. At the same time, the garbage collector must call the "destructor" every time it is working. This obviously affects the performance. If this is not necessary, do not create "destructor" for your class ".
More seriously, if Class A has A "destructor", Class B has A "destructor", and Class A has A class B Variable, when the garbage collector works, the Calling sequence of the "destructor" of A and B is not guaranteed. This will cause errors, which are more serious than performance problems.
Oh, my! Try not to access managed member variables in the Destructor unless the unmanaged resources are destroyed. However, it is usually necessary to determine the destruction of unmanaged resources. Obviously, "destructor" cannot be used to solve the problem.
GC. SuppressFinalize () will prevent Finalize from being called.
 
 
IDisposable Interface
It is better to provide a method that can release resources that need to be released in advance, and then the object can be released when the garbage collector is working. This is the Dispose method.
Class Class1: IDisposable
{
Public Class1 ()
{
GC. SuppressFinalize (this );
Console. WriteLine ("constructor ");
}
 
~ Class1 ()
{
Console. WriteLine ("destructor ");
}
 
Public void Dispose ()
{
GC. SuppressFinalize (this );
Console. WriteLine ("Dispose ");
}
}
GC. suppressFinalize (this); this statement tells the spam manager that from now on, it is not allowed to call my "destructor, of course it cannot be called, because I still need to execute the code to clean up resources after Dispose. If the Garbage Collector grabs the object before it recycles the object, it will cause an error. If the class does not implement the "destructor", of course this sentence is not needed.

 
Close Method
To adapt to the habits of most programmers, you can add a Close method. In fact, you only call Dispose internally. If you are not happy, you may be too lazy to implement it.
 
Using Method
To reduce the compilation of exception handling code, the using syntax can ensure that the Dispose method or Close method is called no matter whether an exception occurs or not, provided that the object supports the Idisposable interface. Therefore, this is a concise syntax.

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.