Tuesday, January 18, 2011

Destructor

Destructor
Destructor have same name as class name. Destructor is used to destruct instance of class. Destructors enable the runtime system to recover the heap space and to terminate file I/O that is associated with the removed class instance. A class can only have one destructor. In c# we can’t define destructor in structs they are only used in classes. Destructor can’t be inherited or overloaded and we can’t call destructor explicitly they are invoked automatically (.Net frameworks Garbage Collector (GC) has control over destructor). Destructor does not have parameter or any modifiers.


 Example
To see working of destructor let us consider a example. Take three classes Human,Male and Stud. Stud is most derived class and Human is least derived class. Every class contain a destructor and we will create instance of most derived class(i.e. Stud) and destructor will called automatically in order from Stud then Male then Human.


Output will be
Student Class Destructor
Male Class Destructor
Human Class Destructor  

No comments:

Post a Comment