Destructors in C# are methods inside the class used to destroy instances of that class when they are no longer needed. The Destructor is called implicitly by the .NET Framework’s Garbage collector and therefore programmer has no control as when to invoke the destructor. An instance variable or an object is eligible for destruction when it is no longer reachable.
Important Points:
- A Destructor is unique to its class i.e. there cannot be more than one destructor in a class.
- A Destructor has no return type and has exactly the same name as the class name (Including the same case).
- It is distinguished apart from a constructor because of the Tilde symbol (~) prefixed to its name.
- A Destructor does not accept any parameters and modifiers.
- It cannot be defined in Structures. It is only used with classes.
- It cannot be overloaded or inherited.
- It is called when the program exits.
- Internally, Destructor called the Finalize method on the base class of object.
Syntax:
class Example
{
// Rest of the class
// members and methods.
// Destructor
~Example()
{
// Your code
}
}
Example 1:
using System;
namespace GeeksforGeeks {
class Complex {
int real, img;
public Complex()
{
real = 0;
img = 0;
}
public void SetValue( int r, int i)
{
real = r;
img = i;
}
public void DisplayValue()
{
Console.WriteLine( "Real = " + real);
Console.WriteLine( "Imaginary = " + img);
}
~Complex()
{
Console.WriteLine( "Destructor was called" );
}
}
class Program {
static void Main( string [] args)
{
Complex C = new Complex();
C.SetValue(2, 3);
C.DisplayValue();
}
}
}
|
Output:
Real = 2
Imaginary = 3
Destructor was called
Explanation: In the above example, the class consists of a constructor Complex(), a SetValue method to set value of the complex class’ instance, a DisplayValue method to display the value of the instance and a Destructor ~Complex() to destroy the object when the instance is no longer required, it prints the message “Destructor was called”, which depends on the programmer what message he/she wants to display or it can also be left blank.
Example 2:
using System;
using System.IO;
namespace GeeksforGeeks {
public class Vect {
double i, j, k;
public Vect()
{
i = 0.0;
j = 0.0;
k = 0.0;
Console.WriteLine( "An instance of " +
"Vect class created" );
}
public void SetVector( double iComponent,
double jComponent,
double kComponent)
{
i = iComponent;
j = jComponent;
k = kComponent;
}
public double FindMagnitude()
{
double m = 0.0;
m = Math.Sqrt(i * i + j * j + k * k);
return m;
}
~Vect()
{
Console.WriteLine( "The instance of" +
" Vect class Destroyed" );
}
}
class Procedure {
static void Main( string [] args)
{
Vect V1 = new Vect();
V1.SetVector(2.3, -1.5, 7.0);
Console.WriteLine( "Magnitude of the vector " +
"2.3i-1.5j+7k is " + V1.FindMagnitude());
}
}
}
|
Output:
An instance of Vect class created
Magnitude of the vector 2.3i-1.5j+7k is 7.51930847884298
The instance of Vect class Destroyed
Explanation: In this example, we have the class Vect which represents a physical quantity a vector. The class consists of three components i, j and k. The methods of the class include the constructor Vect() that sets all components to zero, a SetVector method to set the value of the instance, a FindMagnitude method to calculate the magnitude of the instance with which it is called and a destructor ~Vect() that will destroy the instance V1 when no longer required.
Last Updated :
01 Feb, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...