/// <summary> /// Compares the existing value for the specified key with a specified value, /// and if they are equal, updates the key with a third value. /// </summary> /// <param name="key">The key whose value is compared with comparisonValue and possibly replaced.</param> /// <param name="newValue">The value that replaces the value of the element with key if the comparison results in equality</param> /// <param name="comparisonValue">The value that is compared to the value of the element with key</param> /// <returns>true if the value with key was equal to comparisonValue and replaced with newValue; otherwise, false.</returns> public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue) { bool flag; using (UpgradeableReadLock.Enter(readerWriterLock)) { TValue local; if (!dictionary.TryGetValue(key, out local)) { return(false); } if (!local.Equals(comparisonValue)) { flag = false; } else { using (WriteLock.Enter(readerWriterLock)) { dictionary[key] = newValue; flag = true; } } } return(flag); }
/// <summary> /// Adds a key/value pair to the <see cref="T:ExitGames.Threading.SynchronizedDictionary`2"/> if the key /// does not already exist /// </summary> /// <param name="key">The key of the element to get or add.</param> /// <param name="valueFactory">The function used to generate a value for the key</param> /// <returns>The value for the key. This will be either the existing value for the key if the /// key is already in the dictionary, or the new value for the key as returned by valueFactory /// if the key was not in the dictionary.</returns> public TValue GetOrAdd(TKey key, Func <TKey, TValue> valueFactory) { TValue local2; using (UpgradeableReadLock.Enter(readerWriterLock)) { TValue local; if (dictionary.TryGetValue(key, out local)) { local2 = local; } else { using (WriteLock.Enter(readerWriterLock)) { local = valueFactory(key); dictionary.Add(key, local); local2 = local; } } } return(local2); }