Skip to content

Instantly share code, notes, and snippets.

@DDuarte
Created May 6, 2012 22:49
Show Gist options
  • Save DDuarte/2624878 to your computer and use it in GitHub Desktop.
Save DDuarte/2624878 to your computer and use it in GitHub Desktop.
ConcurrentMultiDictionary - C#
// By Nayd
public class ConcurrentMultiDictionary<TKey, TValue> : ConcurrentDictionary<TKey, List<TValue>>
{
public bool TryAdd(TKey key, TValue value)
{
List<TValue> container;
if (!TryGetValue(key, out container))
{
container = new List<TValue>();
if (!TryAdd(key, container))
return false;
}
container.Add(value);
return true;
}
public bool ContainsValue(TKey key, TValue value)
{
List<TValue> container;
return TryGetValue(key, out container) && container.Contains(value);
}
public List<TValue> GetValues(TKey key)
{
List<TValue> container;
return TryGetValue(key, out container) ? container : new List<TValue>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment