重写 C# 等于运算符是否也适用于测试列表相等性?

Does Overriding C# equals operator work for testing list equality as well?

我有一个 class 像这样覆盖 C# 的 == 运算符

public class Foo
{
    /*Variables and Constructors*/

    public static bool operator ==(Foo a, Foo b) 
    {
         /*Check values for equality*/
    }
}

这是否意味着如果我 运行 这个表达式

List<Foo> listA == List<Foo> listB

会return是真的吗?如果不是,我应该如何测试这些列表是否相等?

否 - List<T> 不会覆盖 Equals,因此它默认使用引用相等性。

一个可能的原因是 "equality" 对集合的定义不同。顺序重要吗?重复项呢?

Linq 提供了一种您可以轻松使用的 SequenceEqual 方法,文档提供了它使用的标准:

[Returns] true if the two source sequences are of equal length and their corresponding elements are equal according to the default equality comparer for their type; otherwise, false.

使用

listA.SequenceEqual(listB);

它将调用类型的默认相等比较器。我很确定这意味着您必须为 class 提供 equals() 方法实现(而不是重载 == 运算符)。