| Start |
|---|
Initializing a Dictionary with a Collection Initializer |
Adding to a Dictionary Alternatively they can be added/set via the an indexer. (An indexer internally looks like a property, having a get and set, but takes a parameter of any type which is specified between the brackets) : Unlike the For thread-safe dictionary use |
| Getting a value from a dictionary Given this setup code: You may want to read the value for the entry with key 1. If key doesn't exist getting a value will throw This has one disadvantage: you will search through your dictionary twice (once to check for existence and one to read the value). For a large dictionary this can impact performance. Fortunately both operations can be performed together: |
Make a Dictionary<string, T> with Case-Insensivitve keys. |
| Enumerating a Dictionary You can enumerate through a Dictionary in one of 3 ways: Using KeyValue pairs Using Keys Using Values |
| ConcurrentDictionary<TKey, TValue> (from .NET 4.0) Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently. Creating an instanceCreating an instance works pretty much the same way as with Adding or UpdatingYou might be surprised, that there is no (1) (2) Adding or updating a value, no matter what was the value if it was already present for given key (1): Adding or updating a value, but now altering the value in update, based on the previous value (1): Using the overload (2) we can also add new value using a factory: Getting valueGetting a value is the same as with the Getting or Adding a valueThere are two mehod overloads, that will get or add a value in a thread-safe manner. Get value with key 2, or add value "Second" if the key is not present: Using a factory for adding a value, if value is not present: |
| Removing from a Dictionary Given this setup code: Use the Executing this code removes the key It's incorrect to try and remove a key by setting the value for the key to This will not remove the key. It will just replace the previous value with a value of To remove all keys and values from a dictionary, use the After executing |
| ContainsKey(TKey) To check if a And check if a The ContainsKey method is available on the generic version |
| Dictionary to List Creating a list of KeyValuePair: Creating a list of keys: Creating a list of values: |
ConcurrentDictionary augmented with Lazy'1 reduces duplicated computation ProblemConcurrentDictionary shines when it comes to instantly returning of existing keys from cache, mostly lock free, and contending on a granular level. But what if the object creation is really expensive, outweighing the cost of context switching, and some cache misses occur? If the same key is requested from multiple threads, one of the objects resulting from colliding operations will be eventually added to the collection, and the others will be thrown away, wasting the CPU resource to create the object and memory resource to store the object temporarily. Other resources could be wasted as well. This is really bad. SolutionWe can combine Caching of XmlSerializer objects can be particularly expensive, and there is a lot of contention at the application startup too. And there is more to this: if those are custom serializers, there will be a memory leak too for the rest of the process lifecycle. The only benefit of the ConcurrentDictionary in this case is that for the rest of the process lifecycle there will be no locks, but application startup and memory usage would be inacceptable. This is a job for our ConcurrentDictionary, augmented with Lazy: |
Read Article
https://develop.horje.com/learn/1434/reference