Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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中GetHashCode()的定义#_C#_Hash_Gethashcode_Iequalitycomparer - Fatal编程技术网

C# C中GetHashCode()的定义#

C# C中GetHashCode()的定义#,c#,hash,gethashcode,iequalitycomparer,C#,Hash,Gethashcode,Iequalitycomparer,C#中的字典使用GetHashCode()检索给定键的哈希代码。我浏览了整个字典类,但是对于GetHashCode()函数没有任何定义。它困扰着我,我不知道如何在这门课上找到它的定义 假设我正在使用Dictionary a=new Dictionary(),那么这个结构中给定键的哈希代码是什么?(例如,假设:a.Add(123,43)。如果存储桶数为50,则此键的哈希代码是多少) 这是字典类的Insert()函数的一部分 private void Insert(TKey key, TValue

C#中的
字典
使用GetHashCode()检索给定键的哈希代码。我浏览了整个
字典类
,但是对于
GetHashCode()
函数没有任何定义。它困扰着我,我不知道如何在这门课上找到它的定义

假设我正在使用
Dictionary a=new Dictionary()
,那么这个结构中给定键的哈希代码是什么?(例如,假设:
a.Add(123,43)
。如果存储桶数为50,则此键的哈希代码是多少)

这是
字典类的
Insert()
函数的一部分

private void Insert(TKey key, TValue value, bool add)
    {
        int freeList;
        if (key == null)
        {
            ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
        }
        if (this.buckets == null)
        {
            this.Initialize(0);
        }
        int num = this.comparer.GetHashCode(key) & 0x7fffffff;
        int index = num % this.buckets.Length;

GetHashCode
是在
TKey
类型上实现的,而不是在
Dictionary
类上实现的

对于您的示例所使用的,其定义为:

public override int GetHashCode()
{
    return this;
}

backet的数量与hashcode值无关。

GetHashCode
是在
TKey
类型上实现的,而不是在
字典
类上实现的

对于您的示例所使用的,其定义为:

public override int GetHashCode()
{
    return this;
}

backet的数量与hashcode值无关。

如果不指定
IEqualityComparer
字典使用
EqualityComparer.Default
。这是一个比较器,它只会对给定参数调用
object.GetHashCode
。因此,请查看
Int32.GetHashCode
如果未指定
IEqualityComparer
字典使用
EqualityComparer.Default
。这是一个比较器,它只会对给定参数调用
object.GetHashCode
。那么看看
Int32.GetHashCode

你说的“给定键的哈希代码”是什么意思?你是说这个吗?我想知道用于生成给定密钥的哈希代码的算法。现在我问这个键是否是整数,它将如何计算?然后它将是整数的散列码。通常,“正如我们所知”,后面是一堆错误信息。无论如何,字典正在使用GetHashCode(),而不是定义它。你说的“给定键的哈希代码”是什么意思?你是说这个吗?我想知道用于生成给定密钥的哈希代码的算法。现在我问这个键是否是整数,它将如何计算?然后它将是整数的散列码。通常,“正如我们所知”,后面是一堆错误信息。无论如何,字典正在使用GetHashCode(),而不是定义它。