Choosing a collection

While choosing a collection type, we have to analyze the scenarios in the application in which we want to use them. The primary difference between these collection types is the way we access elements in them:

  • The array collection needs to have a definite size or length while declaring itself. On the other hand, all other collection types can have their sizes increased dynamically. Also, arrays support random access to the data. This basically implies that as long as the element exists at the specified index in the array, we can access that element without needing to loop through the entire array.
  • Queues and stacks allow us access the elements in a definite manner. While the queue works in a FIFO manner, the stack works in a LIFO manner. 
  • On the other hand, the list and dictionary collection types allow us to configure random access to the elements. Please note that, conceptually, the list does not support random access; however, in C#, the list is maintained as an array. Due to this, it supports random access.
  • One important difference between a list and dictionary is the way they save data as well as the performance. In the dictionary collection, we save data in KeyValuePair. This allows us to maintain the uniqueness of the keys that are present in the dictionary. On the other hand, a list does not provide us with that feature. 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.134.114