gpt4 book ai didi

c# - 为什么不需要覆盖 Equals 方法?

转载 作者:太空宇宙 更新时间:2023-11-03 20:53:50 25 4
gpt4 key购买 nike

众所周知,C# 中的所有内容都派生自具有 Equals 方法和其他一些方法的 Object。

我正在创建一个类,我发现我不必将 override 关键字与 Equals 方法一起使用。我不确定为什么?

public class Employee //: IEquatable<Employee>
{
public int Emp_Id
{
get;
set;
}
public string Emp_name
{
get;
set;
}
public string Dept_name
{
get;
set;
}
public bool Equals(Employee other) //here it should use override
{
return this.Emp_Id.Equals(other.Emp_Id);
}
public override int GetHashCode()
{
return this.Emp_Id.GetHashCode();
}
}

最佳答案

因为您没有覆盖 Equals(object)。由于类型不同,您正在创建 Equals 方法的重载

关于c# - 为什么不需要覆盖 Equals 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52629695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com