Destructors
Can anyone please tell me when/ why to use "Destructors" in C#? Pls give an easy example.
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
Satyapriya NayakRank #47 · 31.5K reputationPosted Jan 20, 2012, 5:21 AM EST
Destructors are used to destruct instances of classes.
When we are using destructors in C#, we have to keep in mind the following things:
A class can only have one destructor.
Destructors cannot be inherited or overloaded.
Destructors cannot be called. They are invoked automatically.
A destructor does not take modifiers or have parameters.
A destructor same name as the name of the class but it starts with the character ~
Posted Jan 20, 2012, 4:56 AM EST
Rashed Mohammad19 reputationPosted Jan 20, 2012, 5:15 AM EST
VulpesRank #55 · 29.2K reputationPosted Jan 20, 2012, 5:34 AM EST
http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx
Sam HobbsRank #107 · 20.5K reputationPosted Jan 20, 2012, 10:43 AM EST
I think the first thing that should be said about Destructors is that they are usually not needed. The most common use of destructors is to do cleanup of unmanaged objects. Managed objects in objects (classes and structs) that we define will do whatever cleanup that they need to do themselves and we do not need to do anything explicitly for the managed objects we use.