There's a really good in-depth article found here around how a concurrent dictionary works, but essentially it's a dictionary that's designed to run safely across multiple threads (as described by the "concurrent" part). There are more details on this found here. This is because a sorted dictionary is much slower than a standard dictionary due to the increased overhead sorting has when adding and fetching items. Sorted dictionaries are really only useful when you need the data to be sorted (an example I've seen is using DateTime as a key and needing to retrieve anything between 2 dates). I've written a DotNetFiddle showing this in practice below: The sorted dictionary is really quite straightforward, it will sort the dictionary based on the value of the key. This dictionary type also lives in the namespace. However, because new dictionaries are created every time an immutable dictionary is updated, there is a performance impact that isn't found on a standard dictionary. This means that the underlying object stays the same and therefore an immutable dictionary is thread-safe compared to previous entries in this list. The main reason to have these methods available is when you want to make data in a dictionary available to callers, but don't want to allow a caller to modify the collection.Īn immutable dictionary works in a very similar manner to the standard dictionary in C# but whenever you add or remove an item from the dictionary, it creates a copy of the dictionary, adds the item and then returns the new collection to you. There is a small difference between these methods as the ReadOnlyDicitonary has a TryAdd method, but attempting to call it will cause a NotSupportedException to be thrown. You can declare this dictionary like this:Įnter fullscreen mode Exit fullscreen mode Given this is the default, I'm going to run through some common features of a dictionary within C#. It's pretty decent in most scenarios where you need one and is also the 'oldest' form of dictionary in C#. Dictionaries are also (usually) part of the generic namespace in C# because the type of the key and value is declared at initialization.Īs I mentioned earlier there's a fair number of types of dictionary in C# so let's go through them now. However, while this was original intent, dictionaries are regularly used for 'simpler' retrieval such as pairs of strings as they are also useful guaranteeing uniqueness in a collection. It does this by allowing you to pull out a key (which has to be unique) that is linked to the larger object (the value). It's mainly used when you need to store large or complex objects within the code while also optimizing for quick retrieval of data. Put simply, a dictionary is a list of key-value pairs. However, there are a lot more versions of dictionary in C# and I haven't an article bringing them all together to discuss where you'd use one over the other, so hopefully this might help you out. Dictionaries in C# are one of the more useful data structures you can use to store and retrieve data quickly and most programming languages have some form of dictionary built into them.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |