Eran Kampf
Eran Kampf
4 min read

[Code Snippet] SyncStringDictionary - A synchronized StringDictionary

Here’s a code snippet for a thread-safe (synchronized) StringDictionary class:

class SyncStringDictionary : StringDictionary
{
  /// <summary>
  /// Initializes a new instance of the SyncStringDictionary class.
  /// </summary>
  public SyncStringDictionary()
  {
  }

  /// <summary>
  /// Gets the number of key/value pairs in the SyncStringDictionary.
  /// </summary>
  /// <returns>The number of key/value pairs in the SyncStringDictionary.Retrieving the value of this property is an O(1) operation.</returns>
  public override int Count
  {
    get { lock (this.SyncRoot) { return base.Count; } }
  }

  /// <summary>
  /// Gets a value that indicates whether access to the SyncStringDictionary is synchronized (thread safe).
  /// </summary>
  /// <returns>true if access to the SyncStringDictionary is synchronized (thread safe); otherwise, false.</returns>
  public override bool IsSynchronized { get { return true; } }
  /// <summary>
  /// Gets a collection of keys in the SyncStringDictionary.
  /// </summary>
  /// <returns>An System.Collections.ICollection that provides the keys in the SyncStringDictionary.</returns>
  ///
  public override System.Collections.ICollection Keys
  {
    get
    {
      lock (this.SyncRoot)
      {
        return base.Keys;
      }
    }
  }

  /// <summary>
  /// Gets a collection of values in the SyncStringDictionary.
  /// </summary>
  /// <returns>An System.Collections.ICollection that provides the values in the SyncStringDictionary.</returns>
  public override System.Collections.ICollection Values { get { lock (this.SyncRoot) { return base.Values; } } }

  /// <summary>
  /// Gets or sets the value associated with the specified key.
  /// </summary>
  /// <param name=”key”>The key whose value to get or set.</param>
  /// <returns>The value associated with the specified key. If the specified key is not found, Get returns null, and Set creates a new entry with the specified key.</returns>
  /// <exception cref=”System.ArgumentNullException”><paramref name=”key”/> is null.</exception>
  public override string this[string key]
  {
    get { lock (this.SyncRoot) { return base[key]; } }
    set { lock (this.SyncRoot) { base[key] = value; } }
  }

  /// <summary>
  /// The value of the entry to add. The value can be null.
  /// </summary>
  /// <param name=”key”>The key of the entry to add.</param>
  /// <param name=”value”>The value of the entry to add. The value can be null.</param>
  /// <exception cref=”System.NotSupportedException”>The SyncStringDictionary is read-only.</exception>
  /// <exception cref=”System.ArgumentException”>An entry with the same key already exists in the SyncStringDictionary.</exception>
  /// <exception cref=”System.ArgumentNullException”><paramref name=”key”/> is null</exception>
  public override void Add(string key, string value)
  {
    lock (this.SyncRoot)
    {
     base.Add(key, value);
    }
  }

  /// <summary>
  /// Removes all entries from the SyncStringDictionary.
  /// </summary>
  /// <exception cref=”System.NotSupportedException”>The SyncStringDictionary is read-only.</exception>
  public override void Clear()
  {
    lock (this.SyncRoot)
    {
     base.Clear();
    }
  }

  /// <summary>
  /// Determines if the SyncStringDictionary contains a specific key.
  /// </summary>
  /// <param name=”key”>The key to locate in the SyncStringDictionary.</param>
  /// <returns>true if the SyncStringDictionary contains an entry with the specified key; otherwise, false.</returns>
  /// <exception cref=”System.ArgumentNullException”><paramref name=”key”/> is null.</exception>
  public override bool ContainsKey(string key)
  {
    lock (this.SyncRoot)
    {
      return base.ContainsKey(key);
    }
  }

  /// <summary>
  /// Determines if the SyncStringDictionary contains a specific value.
  /// </summary>
  /// <param name=”value”>The value to locate in the SyncStringDictionary. The value can be null.</param>
  /// <returns>true if the SyncStringDictionary contains an element with the specified value; otherwise, false.</returns>
  public override bool ContainsValue(string value)
  {
    lock (this.SyncRoot)
    {
     return base.ContainsValue(value);
    }
  }

  /// <summary>
  /// Copies the string dictionary values to a one-dimensional System.Array instance at the specified index.
  /// </summary>
  /// <param name=”array”>The one-dimensional System.Array that is the destination of the values copied from the SyncStringDictionary.</param>
  /// <param name=”index”>The index in the array where copying begins.</param>
  /// <exception cref=”System.ArgumentNullException”><paramref name=”array”/> is null.</exception>
  /// <exception cref=”System.ArgumentOutOfRangeException”><paramref name=”index”/> is less than the lower bound of array.</exception>
  /// <exception cref=”System.ArgumentException”>
  /// <paramref name=”array”/> is multidimensional.-or- The number of elements in the SyncStringDictionary
  /// is greater than the available space from <paramref name=”index”/> to the end of array.
  /// </exception>
  public override void CopyTo(Array array, int index)
  {
    lock (this.SyncRoot)
    {
      base.CopyTo(array, index);
    }
  }

  /// <summary>
  /// Returns an enumerator that iterates through the string dictionary.
  /// </summary>
  /// <returns>An System.Collections.IEnumerator that iterates through the string dictionary.</returns>
  public override System.Collections.IEnumerator GetEnumerator()
  {
    lock (this.SyncRoot)
    {
      return base.GetEnumerator();
    }
  }

  //
  // Summary:
  // Removes the entry with the specified key from the string dictionary.
  //
  // Parameters:
  // key:
  // The key of the entry to remove.
  //
  // Exceptions:
  // System.ArgumentNullException:
  // The key is null.
  //
  // System.NotSupportedException:
  // The SyncStringDictionary is read-only.
  /// <summary>
  /// Removes the entry with the specified key from the string dictionary.
  /// </summary>
  /// <param name=”key”>The key of the entry to remove.</param>
  /// <exception cref=”System.ArgumentNullException”><paramref name=”key”/> is null.</exception>
  /// <exception cref=”System.NotSupportedException”>The SyncStringDictionary is read-only.</exception>
  public override void Remove(string key)
  {
    lock (this.SyncRoot)
    {
      base.Remove(key);
    }
  }
}