1
 foreach (Ant ant in state.MyAnts)
        {
            if (m_foodTasks.ContainsKey(ant))
            {
...

通过调试,我可以看到 m_foodTasks 中有一个蚂蚁,其值完全相同。所以我会假设它比较参考地址..(我是对的吗?)

我如何让它按价值进行比较?

阅读答案后编辑:

信息太多......我会花一些时间来研究这些,但这是蚂蚁班已经拥有的(我不能说所有这些东西是什么):

public class Location : IEquatable<Location> {

    /// <summary>
    /// Gets the row of this location.
    /// </summary>
    public int Row { get; private set; }

    /// <summary>
    /// Gets the column of this location.
    /// </summary>
    public int Col { get; private set; }

    public Location (int row, int col) {
        this.Row = row;
        this.Col = col;
    }

    public override bool Equals (object obj) {
        if (ReferenceEquals (null, obj))
            return false;
        if (ReferenceEquals (this, obj))
            return true;
        if (obj.GetType() != typeof (Location))
            return false;

        return Equals ((Location) obj);
    }

    public bool Equals (Location other) {
        if (ReferenceEquals (null, other))
            return false;
        if (ReferenceEquals (this, other))
            return true;

        return other.Row == this.Row && other.Col == this.Col;
    }

    public override int GetHashCode()
    {
        unchecked {
            return (this.Row * 397) ^ this.Col;
        }
    }
}

public class TeamLocation : Location, IEquatable<TeamLocation> {
    /// <summary>
    /// Gets the team of this ant.
    /// </summary>
    public int Team { get; private set; }

    public TeamLocation (int row, int col, int team) : base (row, col) {
        this.Team = team;
    }

    public bool Equals(TeamLocation other) {
        return base.Equals (other) && other.Team == Team;
    }

    public override int GetHashCode()
    {
        unchecked {
            int result = this.Col;
            result = (result * 397) ^ this.Row;
            result = (result * 397) ^ this.Team;
            return result;
        }
    }
}

public class Ant : TeamLocation, IEquatable<Ant> {
    public Ant (int row, int col, int team) : base (row, col, team) {
    }

    public bool Equals (Ant other) {
        return base.Equals (other);
    }
}
4

2 回答 2

1

检查相等性时需要重写 GetHashCode() 方法。

public class Ant : IEquatable<Ant>
{
    private string _someField;

    public Ant(string someField)
    {
        this._someField = someField;
    }

    #region Equality members

    public bool Equals(Ant other)
    {
        if (ReferenceEquals(null, other))
        {
            return false;
        }
        if (ReferenceEquals(this, other))
        {
            return true;
        }
        return string.Equals(_someField, other._someField);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        if (ReferenceEquals(this, obj))
        {
            return true;
        }
        if (obj.GetType() != this.GetType())
        {
            return false;
        }
        return Equals((Ant) obj);
    }

    public override int GetHashCode()
    {
        return (_someField != null ? _someField.GetHashCode() : 0);
    }

    #endregion
}

您可以选择创建一个 IEqualityComparer 类来为您进行比较。

 private sealed class SomeFieldEqualityComparer : IEqualityComparer<Ant>
    {
        public bool Equals(Ant x, Ant y)
        {
            if (ReferenceEquals(x, y))
            {
                return true;
            }
            if (ReferenceEquals(x, null))
            {
                return false;
            }
            if (ReferenceEquals(y, null))
            {
                return false;
            }
            if (x.GetType() != y.GetType())
            {
                return false;
            }
            return string.Equals(x._someField, y._someField);
        }

        public int GetHashCode(Ant obj)
        {
            return (obj._someField != null ? obj._someField.GetHashCode() : 0);
        }
    }
于 2012-09-07T16:27:13.230 回答
1

您需要正确实现 GetHash 和 Equals 以使类在搜索/字典中表现为“比较值”而不是“比较参考”。

另一个(可能更好的选择)是在创建字典时提供IEqualityComparer 。

示例可以在文章中找到,这里是压缩版本:

Dictionary<Box, String> boxes = 
   new Dictionary<Box, string>(new BoxEqualityComparer());

class BoxEqualityComparer : IEqualityComparer<Box>
{
  public bool Equals(Box b1, Box b2)
  {
    return b1.Height == b2.Height;
  }

  public int GetHashCode(Box bx)
  {
    return bx.Height.GetHashCode();
  }
}
于 2012-09-07T16:27:47.403 回答