Destructor not being called
I am testing a few things and am having trouble getting the destructor to work. It's supposed to print some text when called. Instead nothing happens. Anyone know why?
10 Replies
Finalizers are called by the garbage collector @DevLop games, and there is no guarantee when they'll be called.
In general there are very very few cases where you need a finalizer.
And pretty much all of them involve unmanaged resources.
Finalizers - C#
Finalizers in C# perform any necessary final clean-up when a class instance is being collected by the garbage collector.
given your use of "destructor" do you by chance have a background in C++? @DevLop games
I've used it before
C# does not have RAII like C++ does. Finalizers are not fired deterministically.
If you need deterministic destruction of something in C# use https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-idisposable
System.IDisposable interface - .NET
Learn about the System.IDisposable interface.
Any simple way of doing it?
doing what?
Deleting resources when the project is closed or ended
using statement - ensure the correct use of disposable objects - C#...
Use the C# using statement or declaration to ensure the correct use of disposable objects