public void FlagChange(StatTypes statType) { StatCollection oldCollection; StatCollection newCollection; List flaggedChanges; do { oldCollection = m_statCollection; if(!oldCollection.IsChangeFlagged(statType)) { flaggedChanges = oldCollection.Changes; flaggedChanges.Add(new StatChange(statType)); } newCollection = new StatCollection(oldCollection.Stats, flaggedChanges); } while (Interlocked.CompareExchange(ref m_statCollection, newCollection, oldCollection) != oldCollection); } public class StatCollection { private Dictionary> m_stats; private ReadOnlyCollection m_changes; public StatCollection(IDictionary> stats, IList changes) { m_stats = stats; m_changes = changes; } public IDictionary> Stats { get { return new Dictionary>(m_stats); } } public IList Changes { get { return new ReadOnlyCollection(m_changes); } } /// /// Gets the value for a specific and a specific . /// /// the collection type /// the stat type /// the value for the given collection type/stat type public double GetValue(StatCollectionType collectionType, StatTypes statType) { return m_stats[collectionType][statType]; } /// /// Checks if a change is flagged in the collection. /// /// the stat type /// true if the stat type is flagged; false otherwise public bool IsChangeFlagged(StatTypes statType) { return m_changes.Contains(statType); } }