Well, arrays are compared by reference so we have
int[] a = new int[] {1, 2, 3};
int[] b = new int[] {1, 2, 3}; // same content, different reference
// Prints "No"
Console.WriteLine(a.Equals(b) ? "Yes" : "No");
Dictionary<int[], string> dict = new Dictionary<int[], string>() {{
a, "Some Value"}};
// Prints "Not Found"
Console.WriteLine(dict.TryGetValue(b, out var value) ? value : "Not Found");
So we have to explain .Net how to compare arrays; we can do it with a comparer:
public class SequenceEqualityComparer<T> : IEqualityComparer<IEnumerable<T>> {
public bool Equals(IEnumerable<T> x, IEnumerable<T> y) {
if (ReferenceEquals(x, y))
return true;
else if (null == x || null == y)
return false;
return Enumerable.SequenceEqual(x, y, EqualityComparer<T>.Default);
}
public int GetHashCode(IEnumerable<T> obj) =>
obj == null ? 0 : obj.FirstOrDefault()?.GetHashCode() ?? 0;
}
And we should declare the dictionary while mentioning the comparer:
Dictionary<int[], string> dict =
new Dictionary<int[], string>(new SequenceEqualityComparer<int>()) {
{a, "Some Value"}};
Now we can do business as usual:
// Returns "Some Value"
Console.WriteLine(dict[b]);