Demonstrate a destructor. : destructor « Class « C# / CSharp Tutorial






using System; 
 
class Destruct {  
  public int x;  
  
  public Destruct(int i) {  
    x = i;  
  }    
 
  // called when object is recycled 
  ~Destruct() { 
    Console.WriteLine("Destructing " + x); 
  } 
}    
    
class DestructDemo {    
  public static void Main() {    
    Destruct ob = new Destruct(0); 
 
    for(int i=1; i < 100; i++){ 
       Destruct o = new Destruct(i); 
    }
    Console.WriteLine("Done"); 
  }    
}
Done
Destructing 99
Destructing 0
Destructing 98
Destructing 97
Destructing 96
Destructing 95
Destructing 94
Destructing 93
Destructing 92
Destructing 91
Destructing 90
Destructing 89
Destructing 88
Destructing 87
Destructing 86
Destructing 85
Destructing 84
Destructing 83
Destructing 82
Destructing 81
Destructing 80
Destructing 79
Destructing 78
Destructing 77
Destructing 76
Destructing 75
Destructing 74
Destructing 73
Destructing 72
Destructing 71
Destructing 70
Destructing 69
Destructing 68
Destructing 67
Destructing 66
Destructing 65
Destructing 64
Destructing 63
Destructing 62
Destructing 61
Destructing 60
Destructing 59
Destructing 58
Destructing 57
Destructing 56
Destructing 55
Destructing 54
Destructing 53
Destructing 52
Destructing 51
Destructing 50
Destructing 49
Destructing 48
Destructing 47
Destructing 46
Destructing 45
Destructing 44
Destructing 43
Destructing 42
Destructing 41
Destructing 40
Destructing 39
Destructing 38
Destructing 37
Destructing 36
Destructing 35
Destructing 34
Destructing 33
Destructing 32
Destructing 31
Destructing 30
Destructing 29
Destructing 28
Destructing 27
Destructing 26
Destructing 25
Destructing 24
Destructing 23
Destructing 22
Destructing 21
Destructing 20
Destructing 19
Destructing 18
Destructing 17
Destructing 16
Destructing 15
Destructing 14
Destructing 13
Destructing 12
Destructing 11
Destructing 10
Destructing 9
Destructing 8
Destructing 7
Destructing 6
Destructing 5
Destructing 4
Destructing 3
Destructing 2
Destructing 1








7.14.destructor
7.14.1.Destructors
7.14.2.Simple Finalize
7.14.3.Illustrates a destructor
7.14.4.Demonstrate a destructor.
7.14.5.Use destructor and implement IDisposable
7.14.6.Base destructor
7.14.7.Update static field in the deconstructor