Constructor
what is constructor & deconstructor with example??
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Akkiraju IvaturiRank #473 · 5.3K reputationPosted Aug 11, 2012, 12:22 PM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 11, 2012, 3:05 PM EDT
There are two types: instance and static. The former applies to a specific instance and the latter to the class or struct as a whole. Static constructors cannot be called directly but are called by the system before any instance constructor executes.
A destructor is a special method (sometimes referred to as a finalizer) which provides finalization code for a class instance before the garbage collector destroys it. It cannot be called directly but is called by the system just before the instance is about to be destroyed and its memory reclaimed.
Here's a simple example of all three of these constructs:
The output (the third line only shows after pressing a key) is: