Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何组合类成员的哈希代码?_C#_Hashcode - Fatal编程技术网

C# 如何组合类成员的哈希代码?

C# 如何组合类成员的哈希代码?,c#,hashcode,C#,Hashcode,为类生成哈希代码时,是否可以使用该类成员的哈希代码?下面是一个示例类: class Sample { private readonly string _strA, _strB; public Sample(string strA, string strB) { this._strA = strA; this._strB = strB; } public override int GetHashCode() {

为类生成哈希代码时,是否可以使用该类成员的哈希代码?下面是一个示例类:

class Sample
{
    private readonly string _strA, _strB;
    public Sample(string strA, string strB)
    {
        this._strA = strA;
        this._strB = strB;
    }
    public override int GetHashCode()
    {
        return (this._strA + "###" + this._strB).GetHashCode();
    }
}
我认为只要_strA和_strB都不包含字符串“####”。但我不完全确定,因为我不知道哈希代码是如何在字符串上生成的


我在帖子中看到了一个解决方案,我可以根据自己的目的进行定制,但我认为我的解决方案更简单(只要两个字符串都不包含“###”)。

更好的方法是以数学方式组合哈希代码。在当前代码中,每次调用
GetHashCode
时都会创建一个临时字符串,这可能会导致性能低下

public override int GetHashCode()
{
    // omit null-coalesce if we know them to be non-null
    return (33 * (this._strA ?? "").GetHashCode())
         + (this._strB ?? "").GetHashCode();
}
如果您的类确实是不可变的,那么预先计算hashcode可能值4字节:

private readonly int _hash;

public Sample(string strA, string strB)
{
    this._strA = strA;
    this._strB = strB;
    this._hash = (33 * (this._strA ?? "").GetHashCode())
               + (this._strB ?? "").GetHashCode();
}

public override int GetHashCode()
{
    return this._hash;
}

更好的方法是以数学方式组合哈希代码。在当前代码中,每次调用
GetHashCode
时都会创建一个临时字符串,这可能会导致性能低下

public override int GetHashCode()
{
    // omit null-coalesce if we know them to be non-null
    return (33 * (this._strA ?? "").GetHashCode())
         + (this._strB ?? "").GetHashCode();
}
如果您的类确实是不可变的,那么预先计算hashcode可能值4字节:

private readonly int _hash;

public Sample(string strA, string strB)
{
    this._strA = strA;
    this._strB = strB;
    this._hash = (33 * (this._strA ?? "").GetHashCode())
               + (this._strB ?? "").GetHashCode();
}

public override int GetHashCode()
{
    return this._hash;
}

如果有多个字段构成对象的总体哈希代码,那么一种简单且相当有效的方法是:

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

    hash = hash*23 + field1.GetHashCode();
    hash = hash*23 + field2.GetHashCode();
    hash = hash*23 + field3.GetHashCode();

    // And so on for all applicable fields.
    // field1, field2 and field3 are all class field members.

    return hash;
}

如果有多个字段构成对象的总体哈希代码,那么一种简单且相当有效的方法是:

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

    hash = hash*23 + field1.GetHashCode();
    hash = hash*23 + field2.GetHashCode();
    hash = hash*23 + field3.GetHashCode();

    // And so on for all applicable fields.
    // field1, field2 and field3 are all class field members.

    return hash;
}

例如:它是惯用的。(忽略创建临时字符串以创建散列代码的性能影响)将组件散列代码添加(或异或)在一起更通用(适用于多个字符串)。另外,链接字符串相对比较昂贵,因为它分配了更多的字符串,在GetHashCode中可能应该避免?它是否运行得更快(可能是因为没有连接字符串)?(我应该检查样本中的空值)它是否显示出更少的碰撞?我认为我的解决方案显示的冲突数量与.Net的string.GetHashCode()方法相同。ASCII/English输入的散列码的分布是众所周知的Bernstein散列。例如:它是惯用的。(忽略创建临时字符串以创建散列代码的性能影响)将组件散列代码添加(或异或)在一起更通用(适用于多个字符串)。另外,链接字符串相对比较昂贵,因为它分配了更多的字符串,在GetHashCode中可能应该避免?它是否运行得更快(可能是因为没有连接字符串)?(我应该检查样本中的空值)它是否显示出更少的碰撞?我认为我的解决方案显示的冲突数量与.Net的string.GetHashCode()方法相同。ASCII/English输入的散列码的分布是众所周知的Bernstein散列。似乎是:我不知道如何。我提供了一个在那篇文章中没有提到的解决方案。我只是想知道我的解决方案是否好。我还没有在你的帖子或我的搜索结果中看到过这样的解决方案。据我所知,你知道最佳实践是什么,但不管怎么说,你正在实施一些与最佳实践完全不同的东西,并询问社区这是否可行?如果我的潜在解决方案是某个地方发布的其他潜在解决方案的副本,请查找。那我们就可以把我的问题称为重复问题了。很好的论证。不需要进一步讨论。似乎是重复的:我不知道怎么做。我提供了一个在那篇文章中没有提到的解决方案。我只是想知道我的解决方案是否好。我还没有在你的帖子或我的搜索结果中看到过这样的解决方案。据我所知,你知道最佳实践是什么,但不管怎么说,你正在实施一些与最佳实践完全不同的东西,并询问社区这是否可行?如果我的潜在解决方案是某个地方发布的其他潜在解决方案的副本,请查找。那我们就可以把我的问题称为重复问题了。很好的论证。无需进一步讨论。