Advertisement
~destructor()
{
//statements in destructor
}
Advertisement
// C# Example of destructor
using System;
class A
{
//Instance variable of class A
int num;
//Constructor of class A
A(int a)
{
num = a;
Console.WriteLine("Creating an object" + num);
}
//Destructor of class A
//This may run at the termination of the program
~A()
{
Console.WriteLine("Destructing an object" + num);
}
public static void Main(String[] ar)
{
//A for-loop to create 5 objects of class A
for(int i=0; i<5;i++)
{
//Calling the constructor of class A
//And passing a specific int value
//That is stored in the instance variable, num, of each object.
A ob = new A(i);
}
}
}
Creating an object0
Creating an object1
Creating an object2
Creating an object3
Creating an object4
Destructing an object4
Destructing an object0
Destructing an object3
Destructing an object2
Destructing an object1
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement