When do I have to use a Destructor? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

When do I have to use a Destructor?

2nd Nov 2016, 3:55 PM
Shaswat Sinha
Shaswat Sinha - avatar
7 Answers
+ 4
use it to close a file when the program is closed.
22nd Dec 2016, 6:15 AM
festus mutua muasya
festus mutua muasya - avatar
+ 2
You use a destructor to release unmanaged resources. One major point you need to remember when including logic in a destructor is that it is called by the Garbage Collector. You do not have direct control over when in it is called unless you force garbage collection. GC.Collect (); GC.WaitForPendingFinalizers (); I have a code block on this topic.
4th Nov 2016, 9:13 PM
Michael Dolence
+ 1
Don't take my answer for granted, since I never actually needed to use one, but I'd say its for Memory Management. When you know you will not use this object anymore you can destruct it for more memory space. C# has its own convenient garbage collector, but I can imagine that desctructures are important in game development with c++ to optimize, finalize your code.
2nd Nov 2016, 4:13 PM
Florian Schwarzer
+ 1
Distractors are used for terminating processes and updating the program automatically. for example if you have a Bluetooth or TCP connection you can terminate the connection in the distructor that will be called automatically. this can be helpful if the object is destroyed without your will by GC (garbage collector)
7th Dec 2016, 4:01 AM
milk way
milk way - avatar
0
Destructors are useful to close connections, files, save data, free memory, terminate threads, close sockets, ports and so on.
2nd Nov 2016, 9:39 PM
Юрий Алчинов
Юрий Алчинов - avatar
0
let free some memory (optimize)
11th Nov 2016, 6:32 AM
Jorge Gerardo Ortiz Muñoz
Jorge Gerardo Ortiz Muñoz - avatar
0
Thanks everyone
26th Jan 2017, 8:24 AM
Shaswat Sinha
Shaswat Sinha - avatar