C# – MultiMap Data Structure Implementation

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.firatatagun.net
{
public class MultiMap<T,V>
{

private IDictionary<T, IList<V>> multiMap = new Dictionary<T, IList<V>>();

public void Add(T t, V value)
{

IList<V> list;
if (multiMap.TryGetValue(t, out list))
{
list.Add(value);
}
else
{
list = new List<V>();
list.Add(value);
multiMap.Add(t, list);
}

}

public IEnumerable<T> Keys
{
get
{
return this.multiMap.Keys;
}
}

public IEnumerable<IList<V>> Values
{
get
{
return this.multiMap.Values;
}
}

public IList<V> this[T index]
{
get
{
IList<V> list;
if (multiMap.TryGetValue(index, out list))
{
return list;
}
else
{
list = new List<V>();
return list;
}

}
}

}
}