using System.Collections.Concurrent; using System.Collections.Generic; namespace PCL.Core.Utils.Exts; public static class ConcurrentDictionaryExtension { extension(ConcurrentDictionary dict) where TKey: notnull { public bool CompareAndRemove(TKey key, TValue comparison) => ((ICollection>)dict).Remove(new KeyValuePair(key, comparison)); public TValue? UpdateAndGetPrevious(TKey key, TValue value) { TValue? prevValue = default; dict.AddOrUpdate(key, _ => { prevValue = default; return value; }, (_, existingValue) => { prevValue = existingValue; return value; }); return prevValue; } } }