如何进入接口方法(Equals)

Dan*_*Dan 0 c# debugging interface visual-studio-2010 iequatable

我已经实现了iEquatable接口:

LineItem : IEquatable<LineItem>
Run Code Online (Sandbox Code Playgroud)

但现在我想Equals(...)通过单步调试我的方法.但即使在调试模式下,踩入也不会进入它(即F11),并且在方法中放置一个断点也不会让我进入它.我怎么调试呢?

不是它应该是相关的,但这是我的Equals方法:

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);
            }
Run Code Online (Sandbox Code Playgroud)

编辑: 我现在从我的代码中的其他地方调用方法:

if(!fo.Future.Equals(li))...
Run Code Online (Sandbox Code Playgroud)

但是仍然不允许我调试它.

Eri*_*ert 15

您需要向后退一步,并首先学习如何正确实现相等方法.C#被设计成一个"成功的坑"; 也就是说,你应该自然地"陷入"以正确的方式做事.不幸的是,平等并不是C#的"成功之处"; 语言设计师第一次没能轻易做到这一点.

这是我在覆盖相等时使用的模式.

首先,首先编写一个完成所有操作私有静态方法.其他一切都将使用这种方法.通过早期处理(1)引用相等,以及(2)空检查来启动您的方法.

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 ...
}
Run Code Online (Sandbox Code Playgroud)

好了,现在我们已经拥有了,我们可以用它来实现其他一切.

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);
}
Run Code Online (Sandbox Code Playgroud)

这是正确实施的必要条件IEquatable<T>.Equals.您还应该考虑重写==运算符以保持一致:

public static bool operator ==(Foo x, Foo y)
{
    return MyEquality(x, y);
}
public static bool operator !=(Foo x, Foo y)
{
    return !MyEquality(x, y);
}
Run Code Online (Sandbox Code Playgroud)

现在无论你是否打电话object.Equals(foo, bar),foo.Equals(bar)或者foo == bar,你都有一致的行为.

  • @SolutionYogi:我不认为你疯了.:-)对我来说,问题是参考类型是否应该具有价值平等.我的偏好从最好到最差:(1)引用类型使用引用相等,(2)引用类型对所有形式的相等使用值相等,(3)引用类型使用值等于`Equals`和引用相等为`= =`,和(4)其他一切. (5认同)