How to choose between arrays and collections

Cited from Performance Best Practices at a Glance

Arrays are the fastest of all collection types, so unless you need special functionalities like dynamic extension of the collection, sorting, and searching, you should use arrays. If you need a collection type, choose the most appropriate type based on your functionality requirements to avoid performance penalties.

  • Use ArrayList to store custom object types and particularly when the data changes frequently and you perform frequent insert and delete operations. Avoid using ArrayList for storing strings.
  • Use a StringCollection to store strings.
  • Use a Hashtable to store a large number of records and to store data that may or may not change frequently. Use Hashtable for frequently queried data such as product catalogs where a product ID is the key.
  • Use a HybridDictionary to store frequently queried data when you expect the number of records to be low most of the time with occasional increases in size.
  • Use a ListDictionary to store small amounts of data (fewer than 10 items).
  • Use a NameValueCollection to store strings of key-value pairs in a presorted order. Use this type for data that changes frequently where you need to insert and delete items regularly and where you need to cache items for fast retrieval.
  • Use a Queue when you need to access data sequentially (first in is first out) based on priority.
  • Use a Stack in scenarios where you need to process items in a last–in, first-out manner.
  • Use a SortedList for fast object retrieval using an index or key. However, avoid using a SortedList for large data changes because the cost of inserting the large amount of data is high. For large data changes, use an ArrayList and then sort it by calling the Sort method.

本篇發表於 Microsoft Visual Studio。將永久鏈結加入書籤。


在下方填入你的資料或按右方圖示以社群網站登入: 標誌

您的留言將使用 帳號。 登出 /  變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )


您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s