List Contains always returns false C#

I have a class for which I have implemented a custom hashCode[] and equals[Object] method using the Eclipse method generator. Each object of the class has a String field called mUid which should be unique and is enough to determine equality. The overridden methods look like this:

@Override public int hashCode[] { final int prime = 31; int result = 1; result = prime * result + [[mUid == null] ? 0 : mUid.hashCode[]]; return result; } @Override public boolean equals[Object obj] { if [this == obj] return true; if [obj == null] return false; if [getClass[] != obj.getClass[]] return false; DataSource other = [DataSource] obj; if [mUid == null] { if [other.mUid != null] return false; } else if [!mUid.equals[other.mUid]] return false; return true; }

But for some reason, when I compare two objects of my class, it always returns false. I've stepped through the code with a debugger and can determine that the mUid strings are the same. But my equals[Object] method always returns false at the line

if [!mUid.equals[other.mUid]] {return false;}.

This means the String equals[] method is saying the strings are not equal. But, in the debugger I can see that even their hashcodes are equal.

Here is a debugging screenshot:

Can anyone please explain what is happening? Thanks.

Update: It works!

I should point out that I am not calling the equals[...] method directly, but rather from Listlist.contains[DataSource]. It turns out that if I step through the equals[Object] code, it shows me that it returns false for comparing every object in the list [even if there is a match]. But it seems the List.contains[] method returns the right value [true or false] anyway. Don't know why this is, but it works. I had an extra semicolon after the if[list.contains[dataSource]]; so I could not rightly see that contains[] was working fine. Thanks for your help everyone.

Video liên quan

Chủ Đề