Use the constructor and idisposable interfaces in C # to release resources.

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Namespace idisposedemo
  5. {
  6. Example of class Resource Recycling
  7. {
  8. Public static void main (string [] ARGs)
  9. {
  10. Using (person P = new student ())
  11. {
  12. }
  13. // Person P = new person ();
  14. // P = NULL;
  15. // Console. Read ();
  16. Console. Readline ();
  17. }
  18. }
  19. Class emailinfo: idisposable
  20. {
  21. Public String address;
  22. Public bool isvip;
  23. Public emailinfo (string address, bool isvip)
  24. {
  25. This. Address = address;
  26. This. isvip = isvip;
  27. }
  28. # Region idisposable Member
  29. Public void dispose ()
  30. {
  31. This. Address = NULL;
  32. Console. writeline ("members in email are released ");
  33. }
  34. # Endregion
  35. }
  36. Class person: idisposable
  37. {
  38. Protected bool _ flag = true;
  39. Emailinfo email = new emailinfo ("Aladdin", true );
  40. // Structure
  41. ~ Person ()
  42. {
  43. Console. writeline ("Call destructor ");
  44. This. Dispose (false );
  45. }
  46. // Virtual Method
  47. Protected virtual void dispose (bool istrusteeship)
  48. {
  49. // Whether recycle is performed
  50. If (this. _ flag)
  51. {
  52. If (istrusteeship)
  53. {
  54. // Release managed and unmanaged Resources
  55. This. Email. Dispose ();
  56. Console. writeline ("the database and file stream are not released by the dispose () method ");
  57. }
  58. Else
  59. {
  60. // Release unmanaged Resources
  61. Console. writeline ("release of non-drag destructor such as database and file stream ");
  62. }
  63. }
  64. This. _ flag = false;
  65. }
  66. # Region idisposable Member
  67. Public void dispose ()
  68. {
  69. This. Dispose (true );
  70. // Do not call the Destructor when marking the object back
  71. GC. suppressfinalize (this );
  72. }
  73. # Endregion
  74. }
  75. Class student: person
  76. {
  77. Public String stunum;
  78. ~ Student ()
  79. {
  80. This. Dispose (false );
  81. }
  82. Protected override void dispose (bool istrusteeship)
  83. {
  84. // Whether recycle is performed
  85. If (this. _ flag)
  86. {
  87. If (istrusteeship)
  88. {
  89. // Release managed and unmanaged Resources
  90. // Release the student's own Member reference
  91. Console. writeline ("the student's resources are released ");
  92. }
  93. Else
  94. {
  95. // Release unmanaged Resources
  96. Console. writeline ("managed resources of students are recycled by structure ");
  97. }
  98. }
  99. Base. Dispose (istrusteeship );
  100. }
  101. }
  102. }
  103. /**
  104. In the above example, we design the resource recycling for the subclass and parent classes. When the subclass student is recycled, the method of calling the parent class is displayed, so that the class can be recycled layer by layer.
  105. * After student collection is completed, the instance members of the parent class person are recycled (email)
  106. * _ Flag indicates that the parent class is updated at the end. In this way, it does not matter if multiple collections are called.
  107. */

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.