Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. // Demonstrate a destructor.
  2. using System;
  3.  
  4.  
  5. class Destruct {
  6.  
  7.  
  8. public int x;
  9. public Destruct(int i) {
  10. x = i;
  11. }
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. // Called when object is recycled.
  20. ~Destruct() {
  21. Console.WriteLine("Destructing " + x);
  22.  
  23.  
  24.  
  25. }
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. // Generates an object that is immediately destroyed.
  34. public void Generator(int i) {
  35. Destruct o = new Destruct(i);
  36. }
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. class DestructDemo {
  45.  
  46.  
  47.  
  48. static void Main() {
  49.  
  50.  
  51. int count;
  52. Destruct ob = new Destruct(0);
  53.  
  54.  
  55.  
  56. /* Now, generate a large number of objects. At
  57. some point, garbage collection will occur.
  58. Note: You might need to increase the number
  59. of objects generated in order to force
  60. garbage collection. */
  61. for(count=1; count < 100000; count++)
  62.  
  63.  
  64. ob.Generator(count);
  65. Console.WriteLine("Done");
  66.  
  67.  
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement