Насколько я понимаю, GetHashCode вернет одно и то же значение для двух разных экземпляров, которые имеют одинаковые значения. В этом случае документация MSDN немного нечеткая.Почему хэш-коды разные, если два объекта одного типа имеют одинаковые значения?
Хэш-код - это числовое значение, которое используется для идентификации объекта во время проверки равенства.
Если у меня есть два экземпляра одного и того же типа, и те же значения будут возвращать значение GetHashCode()?
Предполагая, что все значения совпадают, будет ли следующий тест прошлым или неудачным?
SecurityUser имеет только приемники и сеттеры;
[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
}
Где класс 'SecurityUser' пришел? 'GetHashCode' может быть переопределен в производных классах, чтобы возвращать что-либо. Нет никакой гарантии, что его реализация верна. –
@RobertHarvey Неплохо, я должен был указать, это не так. SecurityUser имеет только получатели и сеттеры. –