0
votes

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.

7
Yes, you can not use destructor in you code. A destructor is run when the program explicitly frees an object. A finalizer, by contrast, is executed when the internal garbage collection system frees the object.Evgeny Gavrin
No, destructors as you know them in C++ don't exist in C#. Is there some reason you think you need one?Jim Mischel
I just want to clarify it. In documentation and articles "destructor" and "finalizer" are often used. I don't see any difference between them, because I don't see any way to create a destructor in C#.Sergey
Possible duplicate of stackoverflow.com/questions/1076965/… Some good answers there.dizzwave

7 Answers

4
votes

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.

2
votes

I don't quite agree with your statement:

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.

Yes, you can write a finalizer (destructor) in C#, and it does allow you to clean up any unmanaged resources you need to before the object is garbage collected. However, you should be careful to implement it correctly (the dispose pattern comes to mind).

Now, if you mean does it support deterministic destruction? Not necessarily, if you need to deterministically release resources your best bet is the IDisposable interface and the using block.

1
votes

The closest you get to destructors is the IDisposable interface and its Dispose() method. With the using(){} construct, you can make the Dispose() call deterministic, thus completing the venerable RAII pattern.

1
votes

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

The closest match answer is probably: No, there are no deterministic destructors (as you know them from C++ for example) in C# or any of the languages compiled for the verifiable, safe execution by the CLR.

0
votes

The simple answer is that you may write destructors to help with memory management etc. but you cannot explicitly call them. You implement the IDisposable interface and explicitly call the Dispose() method if you need to. The Destructor is present for garbage collection system to use. It will generally check if your object is already disposed and then call that same Dispose() method.

Basically it's there if you forget to explicitly dispose and the Garbage Colletor has to clean up after you :-)

The visual bit is really irrelevant.

0
votes

The C# language has things called "destructors", whose semantics bear no relationship whatsoever to destructors in C++. While the name is unfortunate, "destructor" is the standard-defined term for the C# syntactical element which starts with a tilde and the class name, and which requests the compiler to generate an override of Finalize() which invokes the supplied code and then invokes base.Finalize(). A rather silly and pointless language construct (simply allowing code to override Finalize() would have been less confusing; binding the name Finalize() into the language would be a minor issue compared to binding in GC.KeepAlive(), and GC.SuppressFinalize(), both of which will generally be required in a properly-written program that uses destructors).

Incidentally, the name "destructor" is ironic, since destructors don't actually destroy objects, but rather provide objects which would otherwise be destroyed with a last-second reprieve to put their affairs in order.

-1
votes

You could implement the IDisposable interface to create a kind of destructor (Dispose() method).