簡體   English   中英

是否有任何理由不使用.NET中的GetHashCode生成條件哈希碼?

[英]Is there any reason not to generate conditional hash codes using GetHashCode in .NET?

在我用作文檔或文檔+頁面標識符的類中,我使用以下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");
}

只要等對象具有相同的哈希碼,您的實現就可以了。 這是唯一的要求。 如何計算哈希碼,即是否使用if語句,並不重要。

(另外,如果哈希代碼在對象的生命周期中從未改變過,那將是很好的,因為它幾乎打破了使用哈希代碼的任何數據結構。最理想的是,對象應該是不可變的。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM