我有一门课叫小组。
class Team
{
public Team(string name)
{
this.Name = name;
this.Wins = 0;
this.Opponents = new HashSet<Team>();
}
public string Name { get; set; }
public int Wins { get; set; }
public HashSet<Team> Opponents { get; set; }
}
每当我试图在另一队的HashSet中添加一个只有一个对手的现有团队时,我就会得到一个堆栈溢出异常,该团队的guestTeam.Opponents.Add(homeTeam);
为零对手。
在这里,hometeam在对手中只有一个对手,而guestTeam.Opponents
仍然是空的。
这是个小测试应用程序。Stacktrace的框架图显示了3.
我怎么会被抛出这样的例外呢?
发布于 2017-08-28 06:03:51
我承认我不能再犯错误了。就我个人而言,我可以适当地执行
: IEquatable<Team>
然后:
public bool Equals(Team other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(this.Name, other.Name) && Equals(this.Opponents, other.Opponents);
}
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((Team)obj);
}
public override int GetHashCode()
{
unchecked
{
return ((this.Name != null ? this.Name.GetHashCode() : 0) * 397) ^ (this.Opponents != null ? this.Opponents.GetHashCode() : 0);
}
}
public static bool operator ==(Team left, Team right)
{
return Equals(left, right);
}
public static bool operator !=(Team left, Team right)
{
return !Equals(left, right);
}
当然,我不能复制,所以它只是拍摄。
https://stackoverflow.com/questions/45919718
复制相似问题