How do destructors work in C# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do destructors work in C#

As title says it. Can someone explain this to me

11th May 2019, 5:34 PM
P-P
P-P - avatar
1 Answer
+ 1
Usually they are not necessary as C# has a garbage collection. If you have to manage your resources a bit more carefully, you can implement IDispose for your class, that's called when the garbage collector runs (make sure, references are set to null). Or you can use ~ClassName() {}. That works like any other destructor (but forces your garbage collector to run, that may lead to heavy runtime impact). Microsoft made an excellent technical blog about garbage collection. It's a must read for every C# developer. https://docs.microsoft.com/de-de/dotnet/standard/garbage-collection/fundamentals#what-happens-during-a-garbage-collection
11th May 2019, 7:17 PM
Daniel Adam
Daniel Adam - avatar