0

I recently came about this problem. I have a base Stamp class. Then I made two inherited classes OwnedStamp and AuctionStamp. I overrided the Equals and GetHashCode method in Stamp class like this:

public override bool Equals(object obj)
        {
            if(obj == null || this.GetType().Equals(obj.GetType()))
            {
                return false;
            }
            else
            {
                Stamp stamp = obj as Stamp;
                return this.Title == stamp.Title;
            }
        }

public override int GetHashCode()
        {
            return Title.GetHashCode();
        }

I also have overloaded these operators in the same class:

public static bool operator ==(Stamp lhs, Stamp rhs)
        {
            return lhs.Equals(rhs);

        }
public static bool operator !=(Stamp lhs, Stamp rhs)
        {
            return !lhs.Equals(rhs);

        }

Then I have this scenario:

OwnedStamp checkStamp = check.GetStampByTitle(stampTitle);
if(checkStamp != null && checkStamp.Price <= toCompare.Price)
{
     picked.Add(check);
}

The checkStamp is not null and it holds the correct informtaion I need, but when i check for the equality for null it throws me the exception. I have other lines with code where I check for equality between two stamps of any type. I have tried couple of different Equals method realizations. The problem consists. I have red that checking equality in base class even if we pass child class to the method shouldn't be a problem. Any help would be apreciated!

EIMA
  • 21
  • 3
  • the `Title.GetHashCode()` looks dangerous - can `Title` be `null` ? also: the `|| this.GetType().Equals(obj.GetType())` means that if it is the **same** type, it'll never match... but isn't that when you *expect* it to match *the most*? btw, I think your `Equals` method should be just `return obj is Stamp stamp && stamp.Title == Title;`, and the `GetHashCode()` should probably be `return Title?.GetHashCode() ?? 0;` – Marc Gravell Apr 06 '20 at 09:54
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) Does this line `Stamp stamp = obj as Stamp;` work correct? Are you sure, that `stamp` isn't null here? – Pavel Anikhouski Apr 06 '20 at 09:55
  • 1
    also, your equality operators are incredibly dangerous; if `lhs` is ever `null`: boom; you may also find it preferable to use `if (checkStamp is object && ...` in the check - this does a safe null check (not a type check, despite what it looks like) – Marc Gravell Apr 06 '20 at 09:56
  • This `this.GetType().Equals(obj.GetType())` seems odd. You're saying it both types are the same then the instances cannot be equal. – Sean Apr 06 '20 at 09:56
  • The `checkStamp is object` helped with the `null` situation. It now works. I also changed the Equals method and it works with it too. That's a relief. Now I also would like to know about the `GetHashCode` situation. The `Title` cannot be null, because constructor has default values set, would still be a good idea to make `GetHashCode` perform a check? – EIMA Apr 06 '20 at 10:49

0 Answers0