public void Add(TKey key, TValue value) { lock (_dictionary) { if (_dictionary.TryGetValue(key, out var trackable) && trackable.State != TrackState.Deleted) { throw new ArgumentException(); } _dictionary[key] = new Trackable { Key = key, Item = value, State = trackable == null ? TrackState.Added : TrackState.Changed }; } }
public TValue GetAndChange(TKey key, Func <TValue> factory = null) { lock (_dictionary) { if (_dictionary.TryGetValue(key, out var trackable)) { if (trackable.State == TrackState.Deleted) { if (factory == null) { throw new KeyNotFoundException(); } trackable.Item = factory(); trackable.State = TrackState.Changed; } else if (trackable.State == TrackState.None) { trackable.State = TrackState.Changed; } } else { trackable = new Trackable { Key = key, Item = TryGetInternal(key) }; if (trackable.Item == null) { if (factory == null) { throw new KeyNotFoundException(); } trackable.Item = factory(); trackable.State = TrackState.Added; } else { trackable.State = TrackState.Changed; } _dictionary.Add(key, trackable); } return(trackable.Item); } }