difference between list and dictionary in c#
difference between list and dictionary in c#
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit DiwanPosted Aug 11, 2018, 2:10 AM
MayankPosted Dec 22, 2016, 4:52 AM
Bikesh SrivastavaPosted Dec 21, 2016, 12:22 PM
List<>andDictionary<,>- 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
IDictionarywhere key would be ObjectId and Value would be an object itself.Some differences:
O(1)time complexity to access an item (value) by a keyKhaja MoizuddinPosted Dec 21, 2016, 12:21 PM
TechnetshadowPosted Dec 21, 2016, 5:44 AM
Mohammed Deeb HammoudehPosted Dec 21, 2016, 4:50 AM
IDictionaryis for key->value maps,ICollectionis for sets of similar objects.ICollectionis an interface for collections of similar objects: the controls on a form, the elements in a list, the attributes in an XML tag, and so on. As of .NET 2.0, there's a generic version, so you can refer to a collection of integers asICollection.IDictionary is an interface for mapping one type of object or value to another. It works like a real dictionary, or a phone book: you have a "key" in mind like a person’s name, and when you look it up, you get some information that’s identified by that key, like an address or phone number. Each key can only be listed once, although two different keys are still allowed to have the same value. This is also generic in .NET 2.0, so a dictionary whose keys are strings and whose values are integers would be
IDictionary.A dictionary is actually a collection of key/value pairs: you can use an
IDictionaryas anICollection> , and you can access the keys and values as separate collections with the Keys and Values properties.Both
ICollectionandIDictionaryare unordered, meaning that although you can retrieve the elements in some order with theCopyTomethod or a foreach loop, that order has no special meaning, and it might change for no apparent reason. That’s the main difference betweenICollectionandIList: a list lets you put items in specific positions, just like an array, and they stay there until you move them.