Note that int[,] arrayOfPairs is a multi-dimensional array. It is not an array of pairs, and it’s not even an array of arrays. When accessing items, you will need two indices, which will basically confuse the relationship those two numbers have as part of their pair.
Instead, you should use an actual array of arrays: int[][]. That way, e.g. the first element of that array will be an array of members of the pair. Of course, this structure will not enforce any dimensions, so you cannot actually be sure that an element of that array is a 2-element array.
int[][] arrayOfPairs = new int[][]
{
new int[] { 5, 2 },
new int[] { 6, 1 },
};
A better solution is to use something that will actually enforce that there is a complete pair, i.e. exactly 2 elements. You can use the Tuple type for this, or even come up with your own type for this purpose. The Tuple type already comes with a good equality comparer, so you can use it well for your purpose. You can even put them in a set for quick membership tests:
var pairs = new HashSet<Tuple<int, int>>()
{
Tuple.Create(5, 2),
Tuple.Create(6, 1)
};
Console.WriteLine(pairs.Contains(Tuple.Create(5, 3))); // false
Console.WriteLine(pairs.Contains(Tuple.Create(5, 2))); // true
One final note: Your original condition does not really make much sense:
if ((x == 5 && y == 2) && (x == 6 && y == 1) && ETC)
It’s impossible for x, y to be 5, 2 and 6, 1 at the same time, so that condition would be always false.
If you have to use .NET 2.0 which does not have tuples, you can simply create your own type for this purpose:
public struct Pair
{
public int X { get; set; }
public int Y { get; set; }
public Pair(int x, int y)
{
X = x;
Y = y;
}
public override bool Equals(object other)
{
var otherPair = other as Pair?;
return otherPair != null && otherPair.Value.X == X && otherPair.Value.Y == Y;
}
public override int GetHashCode()
{
return (17 + X.GetHashCode()) * 23 + Y.GetHashCode();
}
}
And then your example would look like this:
var pairs = new HashSet<Pair>()
{
new Pair(5, 2),
new Pair(6, 1)
};
Console.WriteLine(pairs.Contains(new Pair(5, 3))); // false
Console.WriteLine(pairs.Contains(new Pair(5, 2))); // true
If you want to keep it as an array of arrays, you can also write your own Enumerable.Contains method (LINQ came with .NET 3.5):
public bool Contains(int[][] array, int[] item)
{
foreach (int[] elem in array)
{
if (elem.Length != 2)
throw new ArgumentException("Array element length should be exactly 2", "array");
if (elem[0] == item[0] && elem[1] == item[1])
return true;
}
return false;
}
Used like this:
Console.WriteLine(Contains(arrayOfPairs, new int[] { 5, 2 })); // true
Console.WriteLine(Contains(arrayOfPairs, new int[] { 5, 1 })); // false