Search code examples
c#destructorfinalizer

There's no destructor in Visual C#?


As I understand, the destructor syntax (~ClassName) in C# is a way to write a finalizer. This method becomes Finalize method after compiling to the IL.

So, it means that C# programming language DOES support destructors, but Visual C# as a part of .net framework doesn't allow programmers to use it.

EDIT: I know that it's possible to use IDisposable interface for cleaning unmanaged resources. The question is not about it. The question is about are there destructors in Visual C#? Because the syntax of destructors is a way to write a finalizer => there's no way to define a destructor itself.


Solution

  • Destructors are generally necessary in other languages (such as c++) to clean up memory. Since C# is garbage collected, a destructor is only useful for cleaning up resources that wouldn't otherwise be automatically cleaned up.