+ 1
When do I have to use a Destructor?
7 Answers
+ 4
use it to close a file when the program is closed.
+ 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.
+ 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.
+ 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)
0
Destructors are useful to close connections, files, save data, free memory, terminate threads, close sockets, ports and so on.
0
let free some memory (optimize)
0
Thanks everyone