In C #, how can I overload the Equals statement in my class correctly to work Queue.Contains ()?

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?

+4
source share
6 answers

To override Equals, you need to use object as the parameter type and the override keyword.

so you can try something like

  public override bool Equals(object obj) { return Equals(obj as State); } 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; } 

You should probably also include a test for null

See Recommendations for Overloading Equals () and Operator == (C # Programming Guide)

+2
source

You must override equal.

 public override bool Equals(object s2) { //implementation } 

For best practice, you need to implement some other interfaces, such as: IEquatable IEqualtable<State> and the GetHashCode method. The Equals override method and Equals methods in IEquatable IEqualtable<T> can share a common private Equals method.

0
source

You are missing the keyword override

  public override bool Equals(Object obj) { // fill in the body } 
0
source

You just need to override the virtual method defined in the Object class:

 public override Equals(object other) { if(other is State) return Equals((State)other); return base.Equals(other); } 

You need to use this general overload because this method is used by the Contains method, and just adding an instance method with the same name is not enough.

0
source

Below is the code that will work for you very well:

  public class State : Object { Boolean[,] grid = new Boolean[4,4]; public State(Boolean[,] passedGrid){ //Constructor Array.Copy(passedGrid, grid, 16); } public override bool Equals(Object s2){ //Overloaded equals operator for (int x = 0; x < 4; x++){ for (int y = 0; y < 4; y++){ if (grid[x, y] != ((State)s2).grid[x, y]){ return false; } } } return true; } } class Program { Boolean[,] testArray = new Boolean[4, 4]; public static void Main() { Program p = new Program(); p.testContains(p.testArray); } 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); //b is true here } } } 

Also, check out the link below for recommendations on overriding the Equals method:

http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

0
source

For a class type, the following should apply to define a user equality relation:

  • The class must be immutable and must not encapsulate a mutable state. For an immutable class, it is permissible to refer to mutable objects if and only if such references are used only to encapsulate * the identities * of such objects or their immutable attributes.
  • A class must override `bool Equals (Object Other)` to return `true` when` Other` is an equivalent instance of the class (the instance is equivalent to itself, for mutable types, the instance is * only * a thing that is equivalent to itself).
  • The class must override `int GetHashCode ()` so that any two equivalent instances return the same value and it is preferable that two arbitrarily selected nonequivalent instances would hardly return the same value.
  • If the class is sealed, it can implement `IEquatable` for its own type in addition to doing the above. Unsolicited classes should not implement `IEquatable`.

As for your specific class, I would suggest that since there are only 65,536 possible individual instances, you should just save an Integer that identifies which bit combination is used, compare this to Equals and return it for GetHashCode .

0
source

Source: https://habr.com/ru/post/1438807/


All Articles