The Destructor is used to analyze the instances of the class.
Remarks
The Destructor cannot be defined in the structure. Only destructor can be used for classes.
A class can have only one destructor.
The Destructor cannot be inherited or overloaded.
You cannot call the destructor. They are automatically called.
The Destructor neither have modifiers nor parameters.
For example, the following is a classCarStatement of the Destructor:
C # copy code
ClassCar {~ Car () // destructor {// Cleanup statements...}}
The Destructor implicitly calls finalize to the object's base class. In this way, the previous destructorCodeImplicitly converted to the following code:
Copy code
Protected override void finalize () {try {// cleanup statements...} finally {base. Finalize ();}}
This means recursively calling all instances in the inheritance chainFinalizeMethod (from the largest degree of derivation to the least degree of derivation ).
| Note: |
Empty destructor should not be used. If the class contains destructor,FinalizeAn item is created in the queue. When you call the destructor, the garbage collector is called to process the queue. If the Destructor is empty, it only causes unnecessary performance loss. |
ProgramThe member cannot control when to call the Destructor because it is determined by the garbage collector. The garbage collector checks whether there are objects that are no longer used by the application. If the Garbage Collector considers an object to conform to the destructor, it calls the Destructor (if any) and recycles the memory used to store the object. The Destructor is also called when the program exits.
You can force garbage collection by calling collect, but this should be avoided in most cases, because this will cause performance problems.
Use destructor to release resources
Generally, C # does not require much memory management compared to a development language that does not perform garbage collection during runtime. This is because the. NET Framework Garbage Collector implicitly manages the memory allocation and release of objects. However, when applications encapsulate unmanaged resources such as Windows, files, and network connections, you should use the destructor to release these resources. When the object meets the destructor, the garbage collector runsFinalizeMethod.
Explicit resource release
If your applications are using expensive external resources, we also recommend that you explicitly release resources before the Garbage Collector releases objects. You can implementDisposeMethod to complete this, this method is necessary for object cleaning. This greatly improves the application performance. Even with this explicit control over resources, destructor are also a protection measure that can be usedDisposeClear resources when a method call fails.
For more information about resource cleanup, see the following topics:
Clear unmanaged Resources
Implement the dispose method
Using Statement (C # reference)
Example
The following example creates three classes, which constitute an inheritance chain. ClassFirstIs a base class,SecondYesfromFirstDerived, andThirdYesfromSecondDerived. All three classes have destructor. InMain (). Note: When the program is running, the destructor of the three classes will be automatically called and called in the order from the largest degree of derivation to the smallest degree of derivation.
C # copy code
class first {~ First () {system. console. writeline ( " first's destructor is called ") ;}< span style =" color: Blue "> class second: first {~ Second () {system. console. writeline ( " second's destructor is called ") ;}< span style =" color: Blue "> class third: second {~ Third () {system. console. writeline ( " Third's destructor is called ") ;}< span style =" color: blue "> class testdestructors { static void main () {third T = New third () ;}