How do I properly use List-Vector3-.Contains()?

for some reason I seem to be getting different results with these two different code snippets. In both cases I’m searching a list of Vector3’s for a particular Vector3 VALUE.

In all the code below tempVertexList is defined as :

 List<Vector3> tempVertexList; 

This version uses List.Contains() (Which I though might be faster than “by hand”)

  //this version seem to fail to find matches
      if(tempVertexList.Contains(meshVertices*))*

found=true;
And, this version is “by hand”, which seems to find more matches:
//this version seem to find all matches properly
foreach (Vector3 listVector in tempVertexList)
{
if (listVector == meshVertices*)*
{ found = true; break; }
}
Why do these give different results? Does “Contains” use a different comparison operator? How can I fix that?

That’s because .Contains() ends up using Vector3.Equals() (via EqualityComparer<T>.Default since Vector3 doesn’t implement IEquatable), while manual comparison uses ==. Equals() literally compares the x/y/z, while == compares magnitudes.

Here’s (disassembled) Unity code for Vector3:

public override bool Equals(object other)
{
  if (!(other is Vector3))
    return false;
  Vector3 vector3 = (Vector3) other;
  if (this.x.Equals(vector3.x) && this.y.Equals(vector3.y))
    return this.z.Equals(vector3.z);
  return false;
}

public static bool operator ==(Vector3 lhs, Vector3 rhs)
{
  return (double) Vector3.SqrMagnitude(lhs - rhs) < 0.0 / 1.0;
}

You would need to implement your own EqualityComparer and pass it to the .NET collections during construction if you want to use things that do equality checks.