我目前正在尝试检查我的某个列表是否包含对象。 该列表是由包含2个字段的结构组成的对象的列表。Contains()返回false,即使它应该返回true
我试图运行这个小代码:
if(m_EatingMoves.Contains(i_Move))
{
....
}
但表达式将返回false即使我可以肯定看到调试,我想上移是* m_EatingMove *名单内,我认为这个问题可能是因为我的结构中没有对Equals的重写,所以我在StackOverFlow中发现了一个实现,但表达式仍然返回false。除了实现我自己的Contains()之外的任何想法?
这是结构:
public struct Cell
{
public int Row;
public int Col;
public Cell(int i_Row, int i_Col)
{
this.Row = i_Row;
this.Col = i_Col;
}
public override bool Equals(object obj)
{
if (!(obj is Cell))
return false;
Cell cell = (Cell)obj;
return cell.Col == Col && cell.Row == Row;
}
}
现在我有由上述结构的另一个目的:
public class Move
{
private Board.Cell m_Source;
private Board.Cell m_Destination;
public Move(Board.Cell i_Source, Board.Cell i_Destination)
{
m_Source = i_Source;
m_Destination = i_Destination;
}
....(Properties, etc..)
最后,我们具有通过构造函数中初始化列表
private List<Move> m_EatingMoves
你还实现了'Equals'为'Move'? – 2013-05-08 01:09:46
你的结构是可变的,[通常是不可取](http://stackoverflow.com/questions/441309/why-are-mutable-structs-vil)。 – Blorgbeard 2013-05-08 01:13:22
@EvanTrimboli,现在你提到它了,我实现了Equal而不是Equals for move,但是我应该在签名中使用覆盖词吗? – Steinfeld 2013-05-08 01:13:50