Development environment: .NET 4.0 or higher
Note: The System.Collections.Concurrent namespace within .NET Framework 4. As in the MSDN documentation, ConcurrentDictionary implements a thread-safe collection that can be accessed by multiple threads at the same time. ConcurrentDictionary implements IDictionary<TKey, TValue>, and it has some unique mechanisms for adding, updating key/value. (Below is an explanation of when and what method to use)
Scenario 1: Add a new item, only if the key does not exist in the dictionary... Use TryAdd. TryAdd accepts the key and value added to the dictionary, returns fasle if the key does not exist in the dictionary; Returns true if present. public bool TryAdd(TKey key, TValue value)
Situation 2: Update the existing key in the dictionary's Value... Use TryUpdate. If the dictionary has this key and gives a conditional update, it should give a comparison value in this method that can be updated when the current value is equivalent to the comparison value (TValue comparisonValue). public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue)
Situation 3: A key/value needs to be added to the dictionary; If the key is not sure if it exists in the dictionary; If it exists, update If it doesn't exist, add it...
Use AddOrUpdate. AddOrUpdate has two overloads, and the two overloads return the value of value: The first overloaded parameter is AddOrUpdate(TKey, Func<TKey, TValue>, Func<TKey, TValue, TValue>) The parameters are Key and two delegates; The purpose of the first delegate is to delegate when the key does not exist and return the value (initial value) This delegation Func returns < initial value, key>. The second delegate Func < returns Value, key, oldValue> and executes the first delegate if the key does not exist in the dictionary (if it exists, it does not run; The purpose is to give the value the initial value), and then it will return the value (the initial value of the value); The second delegation is a method for changing the value value, which also requires return value (change the initial value to the final value). Func < return the > Value, key, oldValue public TValue AddOrUpdate(TKey, Func<TKey, TValue>, Func<TKey, TValue, TValue>)
2. The parameter of the second overload is AddOrUpdate(TKey, TValue, Func<TKey, TValue, TValue>) The first parameter is key, the second is when the key doesn't exist, this value is the initial value of value, and the third is delegate is to execute the change (Func< returns Value, key, oldValue>). public TValue AddOrUpdate(TKey, TValue, Func<TKey, TValue, TValue>)
Situation 4: Add the value of key to the dictionary, but if the key already exists, take out the value; Otherwise, it will be added First overload: GetOrAdd(TKEY, Func<TKey, TValue>valueFactory) The first parameter is key, the second parameter is delegated Func<value, key>, when the key does not exist, a variable is passed as key, and the value is changed. and create a key and value, returning a value value.
2. Second Overload: GetOrAdd (TKEY, TValue) The first parameter is key, and the purpose of the second parameter is to create a new value given by the key when it does not exist. Returns value. public TValue GetOrAdd(TKEY,Func<TKey, TValue>valueFactory) public TValue GetOrAdd(TKEY,TValue)
Each operation of AddOrUpdate and GetOrAdd is a method that ConcurrentDictionary has for modifying/writing to the dictionary, which ensures thread safety (it is done in a lockless way, reading data in the dictionary). |