1

在我用作文档或文档+页面标识符的类中,我使用 GetHashCode 的以下实现。它“感觉”是对的,但由于我之前没有真正看到过这种方法中特定领域的条件反射,我想知道是否有理由不这样做。当然 ToString 方法也有同样的条件。

public override int GetHashCode ()
{
    int hash = 0;

    unchecked
    {
        hash = 17;
        hash = hash * 23 + this.ProductManufacturer.Value.GetHashCode();
        hash = hash * 23 + this.ProductName.Value.GetHashCode();
        hash = hash * 23 + this.ProductVersion.Value.GetHashCode();
        hash = hash * 23 + this.Guid.Value.GetHashCode();

        if (this.Type != IdentifierType.Document)
        {
            hash = hash * 23 + this.PageNumber.Value.GetHashCode();
            hash = hash * 23 + this.PageCount.Value.GetHashCode();
        }
    }

    return (hash);
}

根据答案和 Eric 的链接更新了代码

public bool Equals (Identifier other)
{
    return (this.Equals(other, this.Type));
}

public override bool Equals (object obj)
{
    return ((obj is HouseOIdentifier) && (this.Equals(obj as Identifier)));
}

public bool Equals (Identifier other, IdentifierType type)
{
    bool result = false;

    if (object.ReferenceEquals(this, other))
    {
        result = true;
    }
    else if (!object.ReferenceEquals(other, null))
    {
        result
            = (this.Type == other.Type)
            && (this.ProductManufacturer.Key == other.ProductManufacturer.Key)
            && (this.ProductManufacturer.Value == other.ProductManufacturer.Value)
            && (this.ProductName.Key == other.ProductName.Key)
            && (this.ProductName.Value == other.ProductName.Value)
            && (this.ProductVersion.Key == other.ProductVersion.Key)
            && (this.ProductVersion.Value == other.ProductVersion.Value)
            && (this.Guid.Key == other.Guid.Key)
            && (this.Guid.Value == other.Guid.Value)
            ;

        if (type == IdentifierType.Page)
        {
            result
                &= (this.PageNumber.Key == other.PageNumber.Key)
                && (this.PageNumber.Value == other.PageNumber.Value)
                && (this.PageCount.Key == other.PageCount.Key)
                && (this.PageCount.Value == other.PageCount.Value)
                ;
        }
    }

    return (result);
}

public override int GetHashCode ()
{
    int hash = 0;

    unchecked // Overflow is fine, just wrap.
    {
        hash = 17;
        hash = hash * 23 + this.Type.GetHashCode();
        hash = hash * 23 + this.ProductManufacturer.Key.GetHashCode();
        hash = hash * 23 + this.ProductManufacturer.Value.GetHashCode();
        hash = hash * 23 + this.ProductName.Key.GetHashCode();
        hash = hash * 23 + this.ProductName.Value.GetHashCode();
        hash = hash * 23 + this.ProductVersion.Key.GetHashCode();
        hash = hash * 23 + this.ProductVersion.Value.GetHashCode();
        hash = hash * 23 + this.Guid.Key.GetHashCode();
        hash = hash * 23 + this.Guid.Value.GetHashCode();

        if (this.Type == HouseOfSynergy.FastForm.Core.Identifier.EnumType.Page)
        {
            hash = hash * 23 + this.PageNumber.Key.GetHashCode();
            hash = hash * 23 + this.PageNumber.Value.GetHashCode();
            hash = hash * 23 + this.PageCount.Key.GetHashCode();
            hash = hash * 23 + this.PageCount.Value.GetHashCode();
        }
    }

    return (hash);
}

public override string ToString ()
{
    return ("whatever");
}
4

1 回答 1

3

只要相等的对象具有相同的哈希码,您的实现就很好。这是唯一的要求。如何计算哈希码,即是否使用 if 语句,并不重要。

(此外,如果散列码在对象的生命周期内从未改变过,那就太好了,因为它破坏了几乎所有使用散列码的数据结构。最好的对象应该是不可变的。)

于 2013-10-12T11:59:07.437 回答