I created a class state. For State Queue objects, I want to check if the queue already contains a state object of equal value. Two State objects, each of which contains a 2D Boolean array, are equal when all array values ββare equal and in the same order.
Here is my code:
public class State { Boolean[,] grid = new Boolean[4,4]; Public State(Boolean[,] passedGrid){ //Constructor grid = Array.Copy(passedGrid, grid, 16); } public bool Equals(State s2){ //Overloaded equals operator for (int x = 0; x < 4; x++){ for (int y = 0; y < 4; y++){ if (grid[x, y] != s2.grid[x, y]){ return false; } } } return true; } } public void testContains(Boolean[] testArray) { Queue<State> testQueue = new Queue<State>(); State s1 = new State(testArray); State s2 = new State(testArray); testQueue.Enqueue(s1); Boolean b = testQueue.Contains(s2); }
Unfortunately, when testContains () is called, and I check the value of testQueue.Contains (s2) at the end, it still says the test is false, although they have the same array values, and the Equals operator is overloaded for testing to do this. What do I need to do or change to get Queue.Contains to work with my object? I read somewhere that getHashCode () is recommended to overload when Equals is overloaded. Do I need to do this in this case? If so, what should getHashCode () overload be?
source share