DESTRUCTOR
C#
IN
WHAT
IS DESTRUCTOR?
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.
A destructor in C# is declared using a tilde (~)
followed by the class name. Unlike C++ destructors, you
don't explicitly call destructors in C#.
IMPORTANCE
POINT:
• 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
thezTilde 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.
CROSS-SELLING
SYNTAX:
class Example
{
// Rest of the class
// members and methods.
// Destructor
~Example()
{
// Your code
}
}
using System;
class Addition
{
private int a, b;
// Constructor
public Addition(int num1, int num2)
{
a = num1;
b = num2;
Console.WriteLine("Constructor called: Numbers initialized.");
}
// Method to add the numbers
public void Add()
{
int sum = a + b;
Console.WriteLine($"The sum of {a} and {b} is: {sum}");
}
// Destructor
~Addition()
{
Console.WriteLine("Destructor called: Object is being cleaned up.");
}
}
class Program
{
static void Main(string[] args)
{
// Create an instance of the Addition class
Addition addition = new Addition(10, 20);
// Call the Add method
addition.Add();
// The destructor will be called automatically when the program ends or when the object is collected by the garbage collector
}
}
SOURCE CODE:
using System;
class Addition
{
private int a, b;
// Constructor
public Addition(int num1, int num2)
{
a = num1;
b = num2;
Console.WriteLine("Constructor called: Numbers initialized.");
}
// Method to add the numbers
public void Add()
{
int sum = a + b;
Console.WriteLine($"The sum of {a} and {b} is: {sum}");
}
// Destructor
~Addition()
{
Console.WriteLine("Destructor called: Object is being cleaned up.");
}
}
class Program
{
static void Main(string[] args)
{
// Create an instance of the Addition class
Addition addition = new Addition(10, 20);
// Call the Add method
addition.Add();
// The destructor will be called automatically when the program ends or when the object
is collected by the garbage collector
}
}
Constructor called: Numbers initialized.
The sum of 10 and 20 is: 30
Destructor called: Object is being cleaned up.
OUTPUT:
THANK YOU

.NET with C# - Destructor .pptx

  • 1.
  • 2.
    WHAT IS DESTRUCTOR? Destructors inC# 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. A destructor in C# is declared using a tilde (~) followed by the class name. Unlike C++ destructors, you don't explicitly call destructors in C#.
  • 3.
    IMPORTANCE POINT: • A Destructoris 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 thezTilde 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.
  • 4.
    CROSS-SELLING SYNTAX: class Example { // Restof the class // members and methods. // Destructor ~Example() { // Your code } }
  • 5.
    using System; class Addition { privateint a, b; // Constructor public Addition(int num1, int num2) { a = num1; b = num2; Console.WriteLine("Constructor called: Numbers initialized."); } // Method to add the numbers public void Add() { int sum = a + b; Console.WriteLine($"The sum of {a} and {b} is: {sum}"); } // Destructor ~Addition() { Console.WriteLine("Destructor called: Object is being cleaned up."); } } class Program { static void Main(string[] args) { // Create an instance of the Addition class Addition addition = new Addition(10, 20); // Call the Add method addition.Add(); // The destructor will be called automatically when the program ends or when the object is collected by the garbage collector } } SOURCE CODE:
  • 6.
    using System; class Addition { privateint a, b; // Constructor public Addition(int num1, int num2) { a = num1; b = num2; Console.WriteLine("Constructor called: Numbers initialized."); } // Method to add the numbers public void Add() { int sum = a + b; Console.WriteLine($"The sum of {a} and {b} is: {sum}"); } // Destructor ~Addition() { Console.WriteLine("Destructor called: Object is being cleaned up."); } } class Program { static void Main(string[] args) { // Create an instance of the Addition class Addition addition = new Addition(10, 20); // Call the Add method addition.Add(); // The destructor will be called automatically when the program ends or when the object is collected by the garbage collector } }
  • 7.
    Constructor called: Numbersinitialized. The sum of 10 and 20 is: 30 Destructor called: Object is being cleaned up. OUTPUT:
  • 8.