Recently, when looking at the source code of a Dictionary, I suddenly thought that there is a possibility of Implicit Memory leakage in the improper use of the Dictionary.
Simplified use cases
Mr. A is writing A simple book sales system.
The first thing he needs to deal with is the list set corresponding to the order and the order. then he found that he needed a specific memory structure to temporarily store all orders and their accompanying sales bibliography set to reduce the pressure on the database. A thinks of the Dictionary, which is the best structure to store associated data. The Order object is used as the key, and the corresponding sales bibliography Books is used as the value and saved in the Dictionary.
The Order contains the order ID, order person ID, and order time. small A knows that to use the Order object as the key, he must rewrite the GetHashCode () method and Equals () method of the Order class to make the two functions meaningful, rather than accepting the default implementation of the system, this is required by Dictionary. the implementation of this function is as follows:
Order Class
- Internal class Order
- {
- Public int ID {get; set