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 allo开发者_如何学Pythonw 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.
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.
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.
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.
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.
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.
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.
You could implement the IDisposable interface to create a kind of destructor (Dispose() method).
精彩评论