The easiest way is to do:
public IEnumerator GetEnumerator()
{
return Items.GetEnumerator();
}
Or, if you only want the values:
public IEnumerator GetEnumerator()
{
return Items.Values.GetEnumerator();
}
Don't forget to use a custom comparer for your dictionary:
private Dictionary<object[], object> Items = new Dictionary<object[], object>(new ObjectArrayComparer());
With:
public class ObjectArrayComparer : IEqualityComparer<object[]>
{
// Determines whether x and y are equal or not
public bool Equals(object[] x, object[] y)
{
return object.ReferenceEquals(x, y) // Returns true if they are the same array instance
|| (x != null && y != null && x.SequenceEqual(y)); // Returns true if x and y are not null and have the same elements (order dependent)
}
// Function that allow to fastly determine if an element is in a set of not.
// This function must have the following property:
// x.Equals(y) implies GetHashCode(x) == GetHashCode(y)
public int GetHashCode(object[] obj)
{
if (obj == null)
return 0;
// Unchecked sum of the Hash Codes of all elements in obj
return unchecked(obj.Select(o => o != null ? o.GetHashCode() : 0).Aggregate(0, (a, b) => a + b));
}
}
Why?
Try this:
var a = new int[] { 0 };
var b = new int[] { 0 };
Console.WriteLine(a == b); // Returns false
Because the default comparer on an array is a reference comparison. As a
and b
are two instances of int[]
, they have not the same reference.
To change this behavior, you have to specify in the dictionary, how you want to compare the object[]
.