The difference between destructor, Dispose, Close method in C # _c# tutorial

Source: Internet
Author: User
Tags garbage collection

One, close and dispose of the difference between the two methods

When the Close method of the object is called, the object may be reused, and the Dispose method, the object's resource needs to be marked as useless, that is, the object is destroyed and cannot be used again. such as common. NET class library, when the Close method is called, you can reopen a database connection through open, and you can call the Dispose method to mark this object as useless and wait for GC recycle when you don't use the object completely.

The difference between the two and the three is as shown

destructor Dispose method Close method
Significance Destroying objects Destroying objects Close Object Resource
Call mode Cannot be displayed call in GC recycle is called You need to display the call or through the using statement Need to display call
Call time Not sure OK, in the display call or leave the Using program block OK, when the display is invoked

Description of destructors and Dispose

Dispose requires the implementation of the IDisposable interface.
Dispose is called by developer Code, and destructors are invoked automatically by GC.
The Dispose method should release all managed and unmanaged resources. The destructor should only release the unmanaged resources. Because a destructor determines the invocation by GC, the GC calls its destructor when it determines that an object is no longer needed, and there may also be other useful managed resources in the object.
At the end of the Dispose method, add the code "GC". SuppressFinalize (this); ", which tells the GC that you do not need to call the object's destructor again, the GC will still call its destructor when it is judged that the object is no longer useful, although the program does not error, but affects system performance.
The resources released by the destructor and Dispose should be the same so that even if the class consumer does not invoke Dispose, the resource is released in Finalize.
Finalize should not be public.
The call to Dispose method is recommended when the system is freed by the frequent invocation of destructors by the system GC to release resources.
When a Dispose method exists, it should be called because the Finalize release resource is usually slow.

Four, the Close function description

Close this method has different meanings in different classes, and there is no provision for closing to have a special meaning, that is, closed does not necessarily release the resources, you can also have the method "shut the door". However, because close has the meaning of "off", it is usually allowed to release the resource by closing. For example, in a file operation, releasing an object with close appears to be more accurate than the Dispose meaning, so when you design a class, you can set close to public, set Dispose to protected, and then call Dispose by close. That is to say, what does close mean, whether or not it frees up resources, is entirely up to the Class Designer. "Close calls Dispose" is a one-sided approach, said the Internet.   Close in SqlConnection simply means that the database connection is closed and the SqlConnection object resource is not freed. According to experience, close and Dispose are both public, and close does not represent a release of resources, because the class designer should not normally use two public methods to free the same resources.

V. Destructors and Dispose method instances

Copy Code code as follows:
public class Baseresource:idisposable
{
As we said before, the destructor actually rewrites the virtual method in System.Object Finalize, by default, a class is not destructors, that is, when an object is garbage collected, the Finalize method is not invoked
~baseresource ()
{
To keep your code readable and maintainable, don't write code to release unmanaged resources here
Must be called in Dispose (false) to false to tell the Dispose (bool disposing) function to be called from the garbage collector when the finalize is called
Dispose (FALSE);
}
cannot be called directly by the client
If disposing is true, then this method is called directly by the client, and both managed and unmanaged resources can be freed
If disposing is false, then the function is called from the garbage collector when calling Finalize, and no other managed object should be referenced at this time so, only unmanaged resources can be freed
protected virtual void Dispose (bool disposing)
{
So this method is called directly by the client, so both managed and unmanaged resources can be freed
if (disposing)
{
Releasing managed resources
Othermanagedobject.dispose ();
}
Freeing unmanaged Resources
Dounmanagedobjectdispose ();
This method is called directly by the customer, telling the garbage collector to purge itself from the finalization queue, thereby preventing the garbage collector from invoking the Finalize method.
if (disposing)
Gc. SuppressFinalize (this);
}
Can be called directly by the client
public void Dispose ()
{
Must be called in Dispose (true) to tell the Dispose (bool disposing) function to be called directly by the client
Dispose (TRUE);
}
}

The above example achieves the purpose:

1/If the customer does not invoke Dispose () and fails to release the managed and unmanaged resources in a timely manner, there is also a chance to execute Finalize () and release the unmanaged resources at garbage collection time, but cause an idle waste of unmanaged resources not released in time

2/If the customer invokes Dispose (), the managed and unmanaged resources can be freed in time, then the Finalize () is not executed back when the object is garbage collected, improving the efficiency of the unmanaged resources and improving the system performance

At last:

If you use unmanaged resources in your class, consider providing the Close method, and the Open method. and call the Close method in your Dispose method first.

When using an already available class, such as SqlConnection. If you are not using this connection for the time being, consider the close () method. Consider calling the Dispose () method if you are not using it.

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.