IDisposable interface is implemented correctly in C #

Source: Internet
Author: User

Role

The primary purpose of this interface is to release unmanaged resources. When a managed object is no longer being used, the garbage collector automatically frees the memory allocated to the object. However, it is not possible to predict when garbage collection will take place. In addition, the garbage collector is ignorant of unmanaged resources such as window handles or open files and streams.

Check

When running code analysis in Visual Studio, if a class contains a property that implements the IDisposable pattern, it will remind you that the class also needs to implement IDisposable mode, and that implementing IDisposable mode is a fixed pattern. This mode can be more effective in preventing memory leaks, but also in some places more concise writing code, such as the Using keyword is designed for this purpose.

Using keyword

Provides convenient syntax to ensure proper use of IDisposable objects. The following is an example:

  
 
  1. using (Font font = new Font("Arial", 10.0f)) {
  2. byte charset = font.GdiCharSet;
  3. }

The Using keyword guarantees that it must be called when it leaves the using scope font.Dispose . So, if the IDisposable variable is used only locally, it is best to wrap it up so as to prevent a memory leak.

Implement IDisposable

MSDN provides a set of standard implementation templates, addresses in this: Ca1063:implement IDisposable correctly, I slightly modified the implementation of their own:

  
 
  1. /// <summary>
  2. /// @author Easily
  3. /// </summary>
  4. public class BaseObject : IDisposable {
  5. private bool _enabled = true;
  6. public bool enabled {
  7. get { return _enabled; }
  8. }
  9. public void Dispose() {
  10. if (_enabled) {
  11. Dispose(true);
  12. }
  13. }
  14. private void Dispose(bool value) {
  15. if (_enabled) {
  16. _enabled = false;
  17. Destroy();
  18. if (value) {
  19. GC.SuppressFinalize(this);
  20. }
  21. }
  22. }
  23. virtual protected void Destroy() {
  24. // release managed resource here
  25. }
  26. ~BaseObject() {
  27. Dispose(false);
  28. }
  29. }

The resources that need to be freed are Destroy released inside, so the subclass must be rewritten if there are resources that need to be freed Destroy . Destroywill only be called once, and then the enabled property will be set to false , if the child class is calling Async methods during the run, the callback must check the enabled properties.



From for notes (Wiz)

IDisposable interface is implemented correctly in C #

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.