Destructor

In this chapter you will learn:

  1. How to create destructors
  2. Destructor calling sequence
  3. Update static field in the deconstructor

Create destructors

Destructor

A destructor method called just prior to an object's final destruction by the garbage collector. A destructor can be used to ensure that an object terminates cleanly. Destructors have this general form:

~class-name( ) {
  // destruction code
}

class-name is the name of the class.

using System;/*from  ja v  a2 s .com*/

class MyClass
{
  ~MyClass()
  {
    Console.WriteLine("Finalizing");
  }
}

class MainClass
{
  static void Main(string[] args)
  {
    MyClass fc = new MyClass();
    Console.WriteLine("Exiting main");
  }
}

The code above generates the following result.

Destructor calling sequence

using System; // j a  va2s.  co m
 
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"); 
  }    
}

The code above generates the following result.

Destructor and static fields

The following code

public class MyClass
{//from j av  a2  s. c  o m
  private static int numberOfMyClass = 0;

  public MyClass()
  {
    System.Console.WriteLine("Creating a MyClass object");
    numberOfMyClass++;  
  }

  ~MyClass()
  {
    System.Console.WriteLine("Destroying a MyClass object");
    numberOfMyClass--;  // decrement numberOfMyClass
  }

  public static int GetNumberOfMyClass()
  {
    return numberOfMyClass;
  }

}


class MainClass
{

  public static void Main()
  {
    System.Console.WriteLine("MyClass.GetNumberOfMyClass() = " + MyClass.GetNumberOfMyClass());

    MyClass myMyClass = new MyClass();
    System.Console.WriteLine("MyClass.GetNumberOfMyClass() = " + MyClass.GetNumberOfMyClass());

    MyClass myMyClass2 = new MyClass();
    System.Console.WriteLine("MyClass.GetNumberOfMyClass() = " + MyClass.GetNumberOfMyClass());
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to Base destructor got called from child destructor
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances