Skip to content

Instantly share code, notes, and snippets.

@Buildstarted
Created November 3, 2012 00:23
Show Gist options
  • Select an option

  • Save Buildstarted/4005222 to your computer and use it in GitHub Desktop.

Select an option

Save Buildstarted/4005222 to your computer and use it in GitHub Desktop.
public class SafeCollection<T> : ICollection<T>
{
private readonly ConcurrentDictionary<T, bool> _inner = new ConcurrentDictionary<T, bool>();
public void Add(T item)
{
_inner.TryAdd(item, true);
}
public void Clear()
{
_inner.Clear();
}
public bool Contains(T item)
{
return _inner.ContainsKey(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
_inner.Keys.CopyTo(array, arrayIndex);
}
public int Count
{
get
{
return _inner.Count;
}
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
bool value;
return _inner.TryRemove(item, out value);
}
public IEnumerator<T> GetEnumerator()
{
return _inner.Keys.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment