Question

I've implemented the iEquatable interface:

LineItem : IEquatable<LineItem>

But now I want to debug my Equals(...) method by stepping through the code. But even in debug mode, stepping in doesn't go into it (i.e. F11), and putting a breakpoint inside the method doesn't get me into it either. How can I debug it??

Not that it should be relevant but here is my Equals method:

public bool Equals(LineItem other)
            {
                List<bool> individuals = new List<bool>();

                individuals.Add(DateTime.Equals(Expiry, other.Expiry));
                individuals.Add(Code == other.Code);
                individuals.Add(Type == other.Type);
                individuals.Add(Class == other.Class);

                Func<object, object, bool> Compare = delegate(object A, object B)
                {
                    if (A == DBNull.Value || B == DBNull.Value)
                        return A == B;
                    else
                        return (double)A == (double)B;
                };

                individuals.Add(Compare(Strike, other.Strike));
                individuals.Add(Compare(Future, other.Future));
                individuals.Add(Compare(Premium, other.Premium));
                individuals.Add(Compare(Volatility, other.Volatility));
                individuals.Add(Compare(Volume, other.Volume));
                individuals.Add(Compare(OpenInterest, other.OpenInterest));
                individuals.Add(Compare(Delta, other.Delta));

                return !individuals.Contains(false);
            }

EDIT: I'm calling the method from elsewhere in my code like this now:

if(!fo.Future.Equals(li))...

but that still doesn't let me debug it.

Was it helpful?

Solution 2

LineItem.Equals(a, b) is a static method call to Object.Equals(object, object); it isn't your method.

This implementation will call a.Equals(object) if you've overridden it, but you did not override it.

OTHER TIPS

You need to take a big step back and learn how to implement equality methods correctly in the first place. C# was designed to be a "pit of success" language; that is, you should naturally "fall into" doing things the right way. Unfortunately, equality is not a "pit of success" in C#; the language designers failed to make it easy to do it right the first time.

Here's the pattern that I use when I override equality.

First, start by writing a private static method that does everything right. Everything else will use this method. Start your method by dealing with (1) the reference equality early out, and (2) null checks.

private static MyEquality(Foo x, Foo y)
{
  if (ReferenceEquals(x, y)) return true;
  // We now know that they are not BOTH null.  If one is null
  // and the other isn't then they are not equal.
  if (ReferenceEquals(x, null)) return false;
  if (ReferenceEquals(y, null)) return false;
  // Now we know that they are both non-null and not reference equal.
  ... check for value equality here ...
}

OK, now that we have that, we can use that to implement everything else.

public override bool Equals(object y)
{
  return MyEquality(this, y as Foo);
}
public override int GetHashcode()
{
  // Implement GetHashcode to follow the Prime Directive Of GetHashcode:
  // Thou shalt implement GetHashcode such that if x.Equals(y) is true then 
  // x.GetHashcode() == y.GetHashcode() is always also true.
}
public bool Equals(Foo y)
{
  return MyEquality(this, y);
}

That is what is necessary to correctly implement IEquatable<T>.Equals. You should also consider overriding the == operator to be consistent:

public static bool operator ==(Foo x, Foo y)
{
    return MyEquality(x, y);
}
public static bool operator !=(Foo x, Foo y)
{
    return !MyEquality(x, y);
}

Now no matter whether you call object.Equals(foo, bar), foo.Equals(bar), or foo == bar, you have consistent behavior.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top