// Init #endregion #region IDictionary<TKey,TValue> Members #region GetValueAddIfNotExist /// <summary> /// Returns the value of <paramref name="key"/>. If <paramref name="key"/> /// does not exist, <paramref name="func"/> is performed and added to the /// dictionary /// </summary> /// <param name="key">the key to check</param> /// <param name="func">the delegate to call if key does not exist</param> public TValue GetValueAddIfNotExist(TKey key, Func <TValue> func) { // enter a write lock, to make absolutely sure that the key // is added/deleted from the time we check if it exists // to the time we add it if it doesn't exist return(Lock_Dictionary.PerformUsingUpgradeableReadLock(() => { TValue rVal; // if we have the value, get it and exit if (m_Dictionary.TryGetValue(key, out rVal)) { return rVal; } // not found, so do the function to get the value Lock_Dictionary.PerformUsingWriteLock(() => { rVal = func.Invoke(); // add to the dictionary m_Dictionary.Add(key, rVal); // return the value return rVal; }); return rVal; })); }