I rolled my own, but am now going over to check out the wintellect version.
Here is my code. (before I've looked at wintellect btw).
I have 2 tests as well.
You'll have to comment out the textBox1 lines to get it to run fyi.
private void TestItIntString()
{
try
{
DuplicateKeyDictionary<int, string> test = new DuplicateKeyDictionary<int,
string>();
int jobtitle1 = 123;
int jobtitle2 = 234;
int jobtitle3 = 345;
test.Add(jobtitle1, "Emp1");
test.Add(jobtitle1, "Emp2");
test.Add(jobtitle1, "Emp3");
test.Add(jobtitle2, "Emp4");
test.Add(jobtitle2, "Emp5");
test.Add(jobtitle2, "Emp6");
test.Add(jobtitle3, "Emp7");
test.Add(jobtitle3, "Emp8");
test.Add(jobtitle3, "Emp9");
List<string> items = test.FindAll(jobtitle1);
textBox1.Text += System.Environment.NewLine + "---------------------" +
System.Environment.NewLine + System.Environment.NewLine;
foreach (string g in items)
{
textBox1.Text += g + System.Environment.NewLine;
}
bool exists1 = test.Exists(jobtitle3, "Emp7");
textBox1.Text += exists1.ToString() + System.Environment.NewLine;
bool exists2 = test.Exists(jobtitle1, "Emp-------");
textBox1.Text += exists2.ToString() + System.Environment.NewLine;
//expected failure here
test.Add(123, "Emp1");
}
catch (Exception ex)
{
textBox1.Text += System.Environment.NewLine + System.Environment.NewLine +
ex.Message;
}
}
private void TestItGuidGuid()
{
try
{
DuplicateKeyDictionary<Guid, Guid> test = new DuplicateKeyDictionary<Guid,
Guid>();
Guid jobtitle1 = new Guid("00000000-0000-0000-0000-000000000001");
Guid jobtitle2 = new Guid("00000000-0000-0000-0000-000000000002");
Guid jobtitle3 = new Guid("00000000-0000-0000-0000-000000000003");
test.Add(jobtitle1, new Guid("10000000-0000-0000-0000-000000000001"));
test.Add(jobtitle1, new Guid("20000000-0000-0000-0000-000000000001"));
test.Add(jobtitle1, new Guid("30000000-0000-0000-0000-000000000001"));
test.Add(jobtitle2, new Guid("40000000-0000-0000-0000-000000000002"));
test.Add(jobtitle2, new Guid("50000000-0000-0000-0000-000000000002"));
test.Add(jobtitle2, new Guid("60000000-0000-0000-0000-000000000002"));
test.Add(jobtitle3, new Guid("70000000-0000-0000-0000-000000000003"));
test.Add(jobtitle3, new Guid("80000000-0000-0000-0000-000000000003"));
test.Add(jobtitle3, new Guid("90000000-0000-0000-0000-000000000003"));
List<Guid> items = test.FindAll(jobtitle1);
textBox1.Text = "";
foreach (Guid g in items)
{
textBox1.Text += g.ToString("N") + System.Environment.NewLine;
}
bool exists1 = test.Exists(jobtitle3, new
Guid("90000000-0000-0000-0000-000000000003"));
textBox1.Text += exists1.ToString() + System.Environment.NewLine;
bool exists2 = test.Exists(jobtitle1, System.Guid.NewGuid());
textBox1.Text += exists2.ToString() + System.Environment.NewLine;
//expected failure here
test.Add(new Guid("00000000-0000-0000-0000-000000000001"), new
Guid("10000000-0000-0000-0000-000000000001"));
}
catch (Exception ex)
{
textBox1.Text += System.Environment.NewLine + System.Environment.NewLine +
ex.Message;
}
}
internal class InternalHolder<K, T>
{
private K _key;
private T _item;
public K Key
{
get { return _key; }
}
public T Item
{
get { return _item; }
}
public InternalHolder(K key, T item)
{
_key = key;
_item = item;
}
}
public class DuplicateKeyDictionary<K, T>
{
List<InternalHolder<K, T>> _internalHolders = new List<InternalHolder<K,
T>>();
public DuplicateKeyDictionary()
{ }
public List<T> FindAll(K key)
{
List<T> returnItems = new List<T>();
foreach (InternalHolder<K, T> ih in _internalHolders)
{
if (ih.Key.Equals(key))
{
returnItems.Add(ih.Item);
}
}
return returnItems;
}
public bool Exists(K key, T item)
{
foreach (InternalHolder<K, T> ih in _internalHolders)
{
if (ih.Key.Equals(key))
{
if (ih.Item.Equals(item))
{
return true;
}
}
}
return false;
}
public void Add(K key, T item)
{
if (this.Exists(key, item))
{
throw new System.ArgumentException(string.Format("This key/item pair already
exists. ({0},{1})", key.ToString(), item.ToString()));
}
_internalHolders.Add(new InternalHolder<K, T>(key, item));
}
}