Beware of an implicit memory leakage caused by Dictionary

Source: Internet
Author: User

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
  1. Internal class Order
  2. {
  3. Public int ID {get; set
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.