VII. Memory Management and pointers
1. Internal background management
2. Release unmanaged Resources
When defining a category, you can use two mechanisms to automatically release resources:
Declare An destructor (or Terminator) as a member of the class.
Implement the system. idisposable interface in the class
Destructor: similar to a method ~. No return type, no parameters, no access modifier.
C # When the compiler compiles the destructor,CodeCompile the code corresponding to the finalize () method to ensure that the finalize () method of the parent class is executed.
C # The execution of destructor delays the time when the object is finally deleted from the memory. Because the objects without destructor will be deleted from the memory in one processing of the garbage collector, but the objects with destructor need to be processed twice to be deleted: objects are not deleted when the Destructor is called for the first time. Can refer to the introduction of C # garbage collection: Secondary recovery mechanism, destructor, dispose, and finalize Etc .. (http://www.cnblogs.com/sban/archive/2007/12/02/978737.html)
Idisposable interface: in C #, The system. idisposable interface is recommended to replace the destructor.
3. Insecure code
C # Only the Unsafe code can use pointers.
In C #, the * symbol is related to the type, and in C ++, * is related to the variable name. For example, int * PX and Py correspond to int * PX and * py in C ++.
& Indicates "Get address" and converts a value type to a pointer. For example, int is converted to * Int. This operator is called an addressing operator.
* Indicates "Get address content" and converts a pointer to a value data type (for example, * float is converted to float ). this operator is called "indirect addressing operator" (sometimes called "unreferencing operator ")