您必须问自己的问题是,当两个实例相NewChildren
同时?由于您在那里有列表,因此这可能不是一个容易回答的问题。当你定义了这个,你必须在你的类中实现相等方法:
public class NewChildren
{
public double[] fitnessValue{get;set;}
public int[] locationScheme{get;set;}
public double crowdingDistance{get;set;}
public bool override Equals(object other)
{
// ... implement your rules for equality here
}
}
现在,要这样做,您必须始终遵循Microsoft 指南。覆盖平等并不是那么简单,尽管它并不复杂。例如,您将拥有所有具有相同元素的数组:
public bool override Equals(object other)
{
if (other == null || !(other is NewChildren))
{
return false;
}
var another = (NewChildren)other;
return AreEquivalent(this.fitnessValue, another.fitnessValue)
&& AreEquivalent(this.locationScheme, another.locationScheme)
&& AreEquivalent(this.crowdingDistance, another.crowdingDistance);
}
public static bool AreEquivalent<T>(T[] a, T[] b)
{
return a1.OrderBy(a => a).SequenceEqual(a2.OrderBy(a => a));
}
数组相等的实现取自这里。您可以使用此参考对其进行优化。