简体   繁体   English

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

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

In a class I use as a document or document+page identifier, I use the following implementation of GetHashCode. 在我用作文档或文档+页面标识符的类中,我使用以下GetHashCode实现。 It 'felt' right but since I haven't really seen domain-specific conditioning in this method before, I was wondering if there is a reason not to do so. 它感觉'正确',但由于我之前没有真正看到这种方法中特定领域的条件,我想知道是否有理由不这样做。 Of course the ToString method has the same conditioning as well. 当然,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);
}

Updated Code based on answers and Eric's links : 根据答案和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");
}

Your implementation is fine as long as equal objects have the same hash code. 只要等对象具有相同的哈希码,您的实现就可以了。 That's the only requirement there is. 这是唯一的要求。 How you calculate the hash code, that is, whether you use if-statements or not, is not important. 如何计算哈希码,即是否使用if语句,并不重要。

(Also, it would be nice if the hash code never changed during the object's life time, because it breaks pretty much any data structure that uses hash codes. Optimally the object should be immutable.) (另外,如果哈希代码在对象的生命周期中从未改变过,那将是很好的,因为它几乎打破了使用哈希代码的任何数据结构。最理想的是,对象应该是不可变的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM