By Team Escalera
on 31 Oct 2017 03:25 PM
  • interview questions
0 comments

What are destructors in C #

In C # Class is instantiated when Constructors are used but when an Object of a Class is deleted Class Destructors are automatically invoked.

  • There can be only one destructor for a class.
  • A Destructor is not called / cannot be called, it is invoked automatically.
  • A Destructor cannot be modified using modifiers
  • A Destructor cannot have parameters

A Destructor Shares same name with the Class but a “ ~ ”  TILDE is used with it

Let’s see an example:

Classname | ~Classname

Example Program in C#

CLASS PERSON
{
PUBLIC PERSON() {
CONSOLE.WRITELINE(“THIS IS A CONSTRUCTOR”);
}
~PERSON() {
CONSOLE.WRITELINE(“THIS IS A DESTRUCTOR”);
}
}
STATIC VOID MAIN(STRING[] ARGS) {
PERSON RAJAT = NEW PERSON();
}
/*OUTPUTS WILL BE
THIS IS A CONSTRUCTOR
THIS IS A DESTRUCTOR
/*

Comments (0)

Leave A Comment