C # differences between Dispose, destructor, and close,

Source: Internet
Author: User

C # differences between Dispose, destructor, and close,

 

I. Differences between Close and Dispose

After the Close method of the object is called, the object may be used again. In the Dispose method, the resources occupied by this object need to be marked as useless, that is, the object is to be destroyed and cannot be used any more. For example, common. the SqlConnection class in the. Net class library. After the Close method is called, you can re-Open a database connection through Open. When this object is completely unnecessary, you can call the Dispose method to mark this object as useless, wait for GC collection.

 

Ii. Differences between the three

 

 

  Destructor Dispose Method Close Method
Meaning Destroy object Destroy object Disable object Resources
Call Method It cannot be displayed and called. It is called during GC collection. The call or using statement must be displayed. Call to be displayed
Call time Uncertain Are you sure you want to call or exit the using block? OK.

 

Iii. Description of destructor and Dispose

  • The IDisposable interface must be implemented for Dispose.
  • Dispose is called by the developer code, and the Destructor is automatically called by the GC.
  • The Dispose method should release all managed and unmanaged resources. The Destructor should only release unmanaged resources. Because the Destructor are called by GC, when GC judges that an object is no longer needed, it calls the destructor, at this time, this object may also contain other useful managed resources.
  • Add the code "GC" at the end of the Dispose method. suppressFinalize (this); "indicates that GC does not need to call the destructor of the object. Otherwise, GC will still call the Destructor after judging that the object is no longer useful, although the program will not go wrong, it will affect the system performance.
  • The Destructor and Dispose release resources should be the same, so that even if the class user does not call Dispose, the resources will be released in Finalize.
  • Finalize should not be public.
  • Releasing resources through frequent calls of the Destructor by system GC reduces system performance. Therefore, we recommend that you call the Dispose method.
  • When a Dispose method exists, call it because Finalize usually releases resources very slowly.

 

Iv. Description of the Close Function

The method of Close has different meanings in different classes, and there is no rule that requires Close to have a special meaning, that is, Close does not necessarily need to release resources, you can also set the Close method to "Close ". However, since Close means "off", it is usually used to release resources, which is also allowed. For example, in file operations, releasing objects with Close seems more accurate than Dispose. Therefore, when designing 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 and whether it will release resources is entirely determined by the class designer. The method of "Close call Dispose" is one-sided on the Internet. In SqlConnection, Close only indicates that the database connection is closed and the resource of SqlConnection is not released. Based on experience, when both Close and Dispose exist (both public), Close does not mean to release resources because, class designers should not use two public methods to release the same resource.

V. destructor and Dispose method instances

1 public class BaseResource: IDisposable 2... {3 ~ BaseResource () 4... {5 // to ensure code readability and maintainability, do not write code that releases unmanaged resources here. 6 // It must be called in Dispose (false) mode, use false to tell the Dispose (bool disposing) function to call the 7 Dispose (false) function when the Garbage Collector calls the Destructor ); 8} 9 // unable to be directly called by the customer 10 // If disposing is true, then this method is directly called by the customer, then managed, both unmanaged resources can be released. 11 // If disposing is false, the function is called from the garbage collector when Finalize is called. In this case, other hosted objects should not be referenced, only unmanaged resources can be released 12 protected virtual void Dispose (bool disposing) 13... {14 // then this method is directly called by the customer, so the hosted and unmanaged resources can be released 15 if (disposing) 16... {17 // release managed resources 18 OtherManagedObject. dispose (); 19} 20 // release of unmanaged resources 21 DoUnManagedObjectDispose (); 22 // This method is directly called by the customer, tells the Garbage Collector to clear itself from the Finalization queue to prevent the garbage collector from calling the Destructor method. 23 if (disposing) 24 GC. suppressFinalize (this); 25} 26 // you can directly call 27 public void Dispose () 28... {29 // It must be called in the Dispose (true) method. true indicates that the Dispose (bool disposing) function is 30 Dispose (true); 31} 32} directly called by the customer}View Code


Refer:

Managed resources: Resources allocated and released by CLR management, that is, new objects in CLR;

Unmanaged resources: objects not managed by CLR, windows kernel objects, such as files, database connections, sockets, and COM objects;

Suggestions for improving C # program 4: Implementation of the standard Dispose mode in C #

 

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.