我需要重写 Equal 吗?

do I need to override Equal?

我已经阅读了一些关于覆盖 Equal 和 GetHashcode 的内容,但是当我只有一个简单的自己的 Equal 方法时是否需要它,如下所示?

如果我必须覆盖它:

这是为什么? - 我应该在覆盖 GetHashCode() 时使用 Id.GetHashCode() 吗?

public class Foo {
    public Guid Id { get; } = new Guid();

    public bool Equal(Foo other) {
        if (other == null) return false;
        return Id == other.Id;
    }
}

您的代码看起来像您想要实现 IEquatable<Foo> for your object. And MSDN advice to oveeride Object.Equals() 方法:

If you implement IEquatable, you should also override the base class implementations of Object.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable.Equals method. If you do override Object.Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. In addition, you should overload the op_Equality and op_Inequality operators. This ensures that all tests for equality return consistent results.

可以在此处找到指向您的解决方案的另一个指针 - 它解释了您的所有选择 - please have a read

Typically, you implement value equality when objects of the type are expected to be added to a collection of some sort, or when their primary purpose is to store a set of fields or properties. You can base your definition of value equality on a comparison of all the fields and properties in the type, or you can base the definition on a subset.