Small Samples of structure analysis and idisposeable in C #

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Namespace idisposedemo
  5. {
  6. /**
  7. * C # is not recommended to use destructor to release resources, because
  8. * 1 the execution time of the Destructor is unknown and will not be called immediately when the reference fails like C ++ ..
  9. * 2 using the Destructor will result in a waste of performance. The Destructor will make the GC release resources twice,
  10. * If there is no destructor, the GC will be cleared in the heap when the reference is lost. However, if there is a destructor, the first destructor will be executed, second heap release
  11. **/
  12. /*
  13. * The idisposeable interface shows that releasing resources is a C # recommended method, so that there is no problem with the Destructor and GC.
  14. * Class Program
  15. {
  16. Static void main (string [] ARGs)
  17. {
  18. Person P = NULL;
  19. Try
  20. {
  21. P = new person ();
  22. P. Name = "aladdinty ";
  23. P. Age = 20;
  24. }
  25. Finally
  26. {
  27. If (P! = NULL)
  28. {
  29. P. Dispose ();
  30. }
  31. }
  32. }
  33. }
  34. *
  35. * The preceding example is easy to cause code confusion and increase the amount of code.
  36. * Using the using keyword in C # can simplify this process without calling the method manually.
  37. * Using (person P = new person ())
  38. {
  39. }
  40. *
  41. * As long as the using scope ends, the system will automatically call the destruction method.
  42. **/
  43. Class Program
  44. {
  45. Static void main (string [] ARGs)
  46. {
  47. Using (person P = new person ())
  48. {
  49. }
  50. // Person P = NULL;
  51. // Try
  52. //{
  53. // P = new person ();
  54. // P. Name = "aladdinty ";
  55. // P. Age = 20;
  56. //}
  57. // Finally
  58. //{
  59. // If (P! = NULL)
  60. //{
  61. // P. Dispose ();
  62. //}
  63. //}
  64. Console. Read ();
  65. }
  66. }
  67. Class person: idisposable
  68. {
  69. Private string name;
  70. Private int age;
  71. Public Person ()
  72. {
  73. }
  74. // Destructor
  75. ~ Person ()
  76. {
  77. Console. writeline ("the old Na has been recycled ");
  78. }
  79. Public string name
  80. {
  81. Get
  82. {
  83. Return this. Name;
  84. }
  85. Set
  86. {
  87. This. Name = value;
  88. }
  89. }
  90. Public int age
  91. {
  92. Get
  93. {
  94. Return this. Age;
  95. }
  96. Set
  97. {
  98. This. Age = value;
  99. }
  100. }
  101. # Region idisposable Member
  102. Public void dispose ()
  103. {
  104. // Release the unmanaged resources used in this class.
  105. Console. writeline ("which line does not concern when reading files ");
  106. }
  107. # Endregion
  108. }
  109. }

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.