1

私は、私の古いプロジェクトからいくつかのコンテナー クラスを (はるかに再利用可能な) 一般的な等価物で作り直しています。コンテナのタイプ (この場合は)ではなくのタイプを確実にするために、何年も前に邪魔をしたようです。TKeyDictionaryintTTlong

これを書き直して使用できるようにすると、クラスlongの内部で実際に何が起こるのでしょうか? Dictionary64ビットの値型TKeyを32ビットのintに強制的にハッシュコードするだけですか? おそらく次のようなものです:

int hashKey32bit = tkey.GetHashCode();
4

1 に答える 1

1

The GetHashCode method always returns a 32 bit int, no matter what type or system you call it on. Thats the point of it and I don't see anything forcefully there. After all, there is no problem with using any object or struct of any size as key. (If they have a somewhat useful implementation of GetHashCode.)

The dictionary will probably end up with some collisions, but as long as the hash codes are distributed evenly across the 32 bit range thats fine.

Edit

So yes, the dictionary always calls the GetHashCode method, even for int, where it's rather simple:

public override int GetHashCode()
{
  return this;
}

For long (int64) it looks like this:

public override int GetHashCode()
{
  return (int) this ^ (int) (this >> 32);
}
于 2013-06-12T09:07:43.240 に答える