Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# GetHashCode/Equals override not called

I'm facing a problem with GetHashCode and Equals which I have overridden for a class. I am using the operator == to verify if both are equal and I'd expect this would be calling both GetHashCode and Equals if their hash code are the same in order to validate they are indeed equal.

But to my surprise, neither get called and the result of the equality test is false (while it should in fact be true).

Override code:

    public class User : ActiveRecordBase<User>

        [...]

        public override int GetHashCode()
        {
            return Id;
        }

        public override bool Equals(object obj)
        {
            User user = (User)obj;
            if (user == null)
            {
                return false;
            }

            return user.Id == Id;
        }
    }

Equality check:

    if (x == y) // x and y are both of the same User class
    // I'd expect this test to call both GetHashCode and Equals
like image 765
tomzx Avatar asked Nov 06 '10 22:11

tomzx


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

Operator == is completely separate from either .GetHashCode() or .Equals().

You might be interested in the Microsoft Guidelines for Overloading Equals() and Operator ==.

The short version is: Use .Equals() to implement equality comparisons. Use operator == for identity comparisons, or if you are creating an immutable type (where every equal instance can be considered to be effectively identical). Also, .Equals() is a virtual method and can be overridden by subclasses, but operator == depends on the compile-time type of the expression where it is used.

Finally, to be consistent, implement .GetHashCode() any time you implement .Equals(). Overload operator != any time you overload operator ==.

like image 116
Daniel Pryden Avatar answered Sep 27 '22 19:09

Daniel Pryden


perhaps adding one more method in your User class.

    public virtual bool Equals(User other) 
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return other.Id == Id;
    }
like image 27
Justin Soliz Avatar answered Sep 27 '22 18:09

Justin Soliz