Where can I learn about the various types of .NET lists?

前端 未结 10 1127
南笙
南笙 2020-12-12 13:13

Does anyone know a good resource to concisely explain the different types of lists available in C# and when their usage is appropriate?

For example, List, Hashtable,

10条回答
  •  难免孤独
    2020-12-12 13:53

    Hash maps

    • Dictionary
    • Hashtable (Non-generic)

    This is a data structure that allows you to keep key-value pairs. Given a Key that has some way of being ordered, you can insert a Value. A simple example could be a list of students where the Key is the student ID, and the value the student name.

    Random access lists

    • List
    • ArrayList (Non-generic)

    Random access lists are used to store a long list of objects that are to be accessed randomly (i.e. you want to access the n'th element in O(1) time). It is not good if you want to insert/delete elements in the middle of the list, since that would require the entire list to be shuffled around which could take some time.

    Linked lists and similar

    • LinkedList
    • Queue
    • Stack

    Linked lists are great if you don't want to access elements in the middle, since that would take O(N) time. It's great if you want to insert/remove elements in the middle since it only involves changing a few pointers.

    Queues and Stacks are slightly specialised, in that they are optimised for FIFO and FILO behaviour (First-In-First-Out and First-In-First-Out respectively).

提交回复
热议问题