In fact, you don't define which criteria two lists are considered equal.
That means .NET check if two lists have the same references in the memory by default because List
is reference type
Obliviously, each list has his memory. So your list has 1205 elements, it returns 1205 element distinct.
As your description, I think your criteria is : 2 lists containing the same elements should be equal.
The Distinct
can receive a IEqualityComparer
, so the idea is : implementation IEqualityComparer
for List<double>
class NumberDoubles: IEqualityComparer<List<double>>
{
public bool Equals(List<double> x, List<double> y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
if (x.Count!= y.Count)
return false;
//Check whether the arrays' values are equal.
for(int i = 0; i < x.Count; i++){
if(x[i] != y[i])
return false;
}
// If got this far, arrays are equal
return true;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(List<double> doubleArray)
{
//Check whether the object is null
if (Object.ReferenceEquals(doubleArray, null)) return 0;
//Calculate the hash code for the array
int hashCode = 0;
bool isFirst = true;
foreach(int i in doubleArray){
if(isFirst) {
hashCode = i;
isFirst = false;
}
else
{
hashCode = hashCode ^ i;
}
}
return hashCode;
}
}
and your code:
genesUsingCrossover.Distinct(new NumberDoubles());