Back to directory
Concept and understanding
First of all, constructor and destructor refer to classes. Therefore, the following statements are for class objects.
Contructor is a Contructor called by the Americans. When a Class Object performs a new operation, it will execute the specified constructor according to your formal parameters.
The Destructor is called Destructors by Americans. It refers to the method to be executed when the class in which it belongs is ineffective. It is generally used to clean up some things, release the memory it occupies (the occupied memory is occupied after the new operation.. net ).
Execution sequence of constructor and destructor when class inheritance occurs
1. Execution of constructor: the constructor of the base class is always executed before the constructor of the base class is executed.
2. Execute the Destructor: You will always first execute your own destructor (release your own class resources) before executing the destructor of its base class.
Let's take a look at the code below:
~~
Check the execution result.
Generally, when using unmanaged resources in a project, we should rewrite the class destructor to manually release resources and disable unnecessary resources.
~ Dispose (IDisposable Member)
In fact. net, there are a lot of unmanaged resources (.. net resources cannot be controlled during runtime. It may be associated with the local network, local computer, network server, data server, and network topology.
). They are File objects, database objects (DbConnection), network Socket objects (Socket), and so on.
Back to directory