当两个相同类型的对象具有相同的值时,为什么哈希码不同?

Chu*_*way 4 .net c# gethashcode

据我所知,GetHashCode将为两个共享相同值的不同实例返回相同的值.在这一点上,MSDN文档有点模糊.

哈希码是一个数值,用于在相等测试期间标识对象.

如果我有两个相同类型和相同值的实例,GetHashCode()将返回相同的值?

假设所有值都相同,以下测试会过去还是失败?

SecurityUser只有getter和setter;

    [TestMethod]
    public void GetHashCode_Equal_Test()
    {
        SecurityUser objA = new SecurityUser(EmployeeName, EmployeeNumber, LastLogOnDate, Status, UserName);
        SecurityUser objB = new SecurityUser(EmployeeName, EmployeeNumber, LastLogOnDate, Status, UserName);

        int hashcodeA = objA.GetHashCode();
        int hashcodeB = objB.GetHashCode();

        Assert.AreEqual<int>(hashcodeA, hashcodeB);
    }


/// <summary>
/// This class represents a SecurityUser entity in AppSecurity.
/// </summary>
public sealed class SecurityUser
{
    #region [Constructor]

    /// <summary>
    /// Initializes a new instance of the <see cref="SecurityUser"/> class using the
    /// parameters passed.
    /// </summary>
    /// <param name="employeeName">The employee name to initialize with.</param>
    /// <param name="employeeNumber">The employee id number to initialize with.</param>
    /// <param name="lastLogOnDate">The last logon date to initialize with.</param>
    /// <param name="status">The <see cref="SecurityStatus"/> to initialize with.</param>
    /// <param name="userName">The userName to initialize with.</param>        
    public SecurityUser(
        string employeeName,
        int employeeNumber,            
        DateTime? lastLogOnDate,
        SecurityStatus status,
        string userName)
    {
        if (employeeName == null)
            throw new ArgumentNullException("employeeName");

        if (userName == null)
            throw new ArgumentNullException("userName");

        this.EmployeeName = employeeName;
        this.EmployeeNumber = employeeNumber;
        this.LastLogOnDate = lastLogOnDate;
        this.Status = status;
        this.UserName = userName;
    }

    #endregion

    #region [Properties]

    /// <summary>
    /// Gets the employee name of the current instance.
    /// </summary>
    public string EmployeeName { get; private set; }

    /// <summary>
    /// Gets the employee id number of the current instance.
    /// </summary>
    public int EmployeeNumber { get; private set; }

    /// <summary>
    /// Gets the last logon date of the current instance.
    /// </summary>
    public DateTime? LastLogOnDate { get; private set; }

    /// <summary>
    /// Gets the userName of the current instance.
    /// </summary>
    public string UserName { get; private set; }

    /// <summary>
    /// Gets the <see cref="SecurityStatus"/> of the current instance.
    /// </summary>
    public SecurityStatus Status { get; private set; }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

Cli*_*int 8

框架为您的自定义对象计算的哈希代码不保证完全相同.

我相信这是由于框架没有走遍你所有的领域,并且计算他们的哈希码,对每个对象来说这都是一件非常耗时的事情(我可能错了).

这就是为什么建议您覆盖自己类型的Equals()GetHashCode()方法.

请参阅:重写GetHashCode

  • 当你不重写`GetHashCode`时生成的哈希码是`object.GetHashCode`,它只是根据引用返回一个CLR确定的数字. (8认同)