title: How to implement ConcurrentHashSet in .Net
body: <p>I am trying to implement a ConcurrentHashSet in the spirit of ConcurrentDictionary,
  approach taken is to use a internal backing ConcurrentDictionary and write small delegating methods, this is how far i got, but well the set theoretic methods are I am stuck on, esp. I am not sure if I can use a foreach and still not violate concurrency</p>
  
  <pre><code>public class ConcurrentHashSet&lt;TElement&gt; : ISet&lt;TElement&gt;
  {
      private readonly ConcurrentDictionary&lt;TElement, object&gt; _internal;
  
      public ConcurrentHashSet(IEnumerable&lt;TElement&gt; elements = null)
      {
          _internal = new ConcurrentDictionary&lt;TElement, object&gt;();
          if (elements != null)
              UnionWith(elements);
      }
  
      public void UnionWith(IEnumerable&lt;TElement&gt; other)
      {
          if (other == null) throw new ArgumentNullException("other");
  
          foreach (var otherElement in other)
              Add(otherElement);
      }
  
      public void IntersectWith(IEnumerable&lt;TElement&gt; other)
      {
          throw new NotImplementedException();
      }
  
      public void ExceptWith(IEnumerable&lt;TElement&gt; other)
      {
          throw new NotImplementedException();
      }
  
      public void SymmetricExceptWith(IEnumerable&lt;TElement&gt; other)
      {
          throw new NotImplementedException();
      }
  
      public bool IsSubsetOf(IEnumerable&lt;TElement&gt; other)
      {
          throw new NotImplementedException();
      }
  
      public bool IsSupersetOf(IEnumerable&lt;TElement&gt; other)
      {
          throw new NotImplementedException();
      }
  
      public bool IsProperSupersetOf(IEnumerable&lt;TElement&gt; other)
      {
          throw new NotImplementedException();
      }
  
      public bool IsProperSubsetOf(IEnumerable&lt;TElement&gt; other)
      {
          throw new NotImplementedException();
      }
  
      public bool Overlaps(IEnumerable&lt;TElement&gt; other)
      {
          return other.Any(otherElement =&gt; _internal.ContainsKey(otherElement));
      }
  
      public bool SetEquals(IEnumerable&lt;TElement&gt; other)
      {
          int otherCount = 0;
          int thisCount = Count;
          foreach (var otherElement in other)
          {
              otherCount++;
              if (!_internal.ContainsKey(otherElement))
                  return false;
          }
          return otherCount == thisCount;
      }
  
      public bool Add(TElement item)
      {
          return _internal.TryAdd(item, null);
      }
  
      public void Clear()
      {
          _internal.Clear();
      }
  
      // I am not sure here if that fullfills contract correctly
      void ICollection&lt;TElement&gt;.Add(TElement item)
      {
          Add(item);
      }
  
      public bool Contains(TElement item)
      {
          return _internal.ContainsKey(item);
      }
  
      public void CopyTo(TElement[] array, int arrayIndex)
      {
          _internal.Keys.CopyTo(array, arrayIndex);
      }
  
      public bool Remove(TElement item)
      {
          object ignore;
          return _internal.TryRemove(item, out ignore);
      }
  
      public int Count
      {
          get { return _internal.Count; }
      }
  
      public bool IsReadOnly
      {
          get { return false; }
      }
  
      public IEnumerator&lt;TElement&gt; GetEnumerator()
      {
          return _internal.Keys.GetEnumerator();
      }
  
      IEnumerator IEnumerable.GetEnumerator()
      {
          return GetEnumerator();
      }
  }
  </code></pre>