what is the difference between list<> and dictionary<> in c#

前端 未结 5 2184
孤独总比滥情好
孤独总比滥情好 2020-12-08 16:00

I have a strange doubt regarding list and dictionary in c#

In a list we add items to list by using the following method

using System.Collections.Gen         


        
5条回答
  •  时光说笑
    2020-12-08 16:31

    List<> and Dictionary<,> - pretty different data structures which used for different purposes, List is simply a set of items and Dictionary is a set of key-value pairs.

    Dictionary is pretty useful when you have a set of complex objects and want to have fast access by let's say ObjectName/ObjectId, in this case you create IDictionary where key would be ObjectId and Value would be an object itself.

    Some differences:

    • List persist order of the items, Dictionary does not
    • List allow fast access by index
    • List support built in QuickSort algorithm for fast data sorting
    • Dictionary allows ~O(1) time complexity to access an item (value) by a key

提交回复
热议问题