C
C#3mo ago
DevLop games

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?
No description
10 Replies
jcotton42
jcotton423mo ago
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.
jcotton42
jcotton423mo ago
Finalizers - C#
Finalizers in C# perform any necessary final clean-up when a class instance is being collected by the garbage collector.
jcotton42
jcotton423mo ago
given your use of "destructor" do you by chance have a background in C++? @DevLop games
DevLop games
DevLop gamesOP3mo ago
I've used it before
jcotton42
jcotton423mo ago
C# does not have RAII like C++ does. Finalizers are not fired deterministically.
jcotton42
jcotton423mo ago
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.
DevLop games
DevLop gamesOP3mo ago
Any simple way of doing it?
jcotton42
jcotton423mo ago
doing what?
DevLop games
DevLop gamesOP3mo ago
Deleting resources when the project is closed or ended
jcotton42
jcotton423mo ago
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
Want results from more Discord servers?
Add your server