DateTime.Now 是否有自己的 GetHashCode 实现来提供唯一的哈希值?

发布于 2024-08-16 22:48:51 字数 211 浏览 8 评论 0原文

MSDN 文章此处指出,默认实现GetHashCode() 的方法不保证结果唯一,不应用作标识符。所以我的问题是 DateTime.Now 是否有自己的实现可以给出唯一的哈希值。谢谢帮助

The MSDN article here states, that the default implementation of GetHashCode() does not guarantee unique results and should be not used as an identifier. So my question is whether DateTime.Now has its own implementation that would give out unique hashes. Thx for help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霊感 2024-08-23 22:48:51

首先,依赖于 DateTimeGetHashCode 的特定实现是错误的。那是对你隐藏的东西。依赖隐藏的细节是一种糟糕的代码味道;他们随时可能改变你的行为并破坏你的代码。

其次,事实证明 DateTime 内部存储了一个 64 位整数 DateTime.Ticks 测量自纪元(0001 年 1 月 1 日午夜)以来 100 纳秒单位的数量。因此,DateTime 实例至少需要 64 位信息。但哈希码是 32 位整数,因此哈希码不可能是唯一的(无法将 64 位空间映射到 32 位空间而不发生冲突)。

明确地说,您可以查看 DateTime.GetHashCode 的源代码:

public override int GetHashCode() {
    long internalTicks = this.InternalTicks;
    return (((int) internalTicks) ^ ((int) (internalTicks >> 0x20)));
}

如您所见,它执行了一些“折叠”操作,将 InternalTicks 压缩为 32 位整数。

一般来说,不要依赖哈希码是唯一的。输入空间通常大于被散列到的空间(所有32位整数的空间)。

如果您绝对必须有一个唯一的键来表示 DateTime 对象,请使用 DateTime.ToBinary。这将为您提供一个唯一的 64 位整数,可用于重构 DateTime(使用 DateTime.FromBinary)。

First, it would be a mistake to rely on the particular implementation of GetHashCode for DateTime. That is something that is hidden from you. Relying on hidden details is a bad code smell; they could change on you at any moment and break your code.

Second, it turns out that DateTime internally stores a 64-bit integer DateTime.Ticks that measures the number of 100-nanosecond units since the epoch (midnight on January 1, 0001). Therefore, DateTime instances require at least 64-bits of information. But hash codes are 32-bit integers and therefore hash codes can not be unique (you can not map 64-bit space to 32-bit space without collisions).

To be explicit, you can see the source code for DateTime.GetHashCode:

public override int GetHashCode() {
    long internalTicks = this.InternalTicks;
    return (((int) internalTicks) ^ ((int) (internalTicks >> 0x20)));
}

As you can see, it does some "folding" to squeeze InternalTicks into a 32-bit integer.

In general, do not rely on hash codes being unique. The input space is generally larger than the space being hashed to (the space of all 32-bit integers).

If you absolutely must have a unique key to represent a DateTime object, use DateTime.ToBinary. This will provide you with a 64-bit integer that is unique and can be used to reconstitute the DateTime (use DateTime.FromBinary).

相权↑美人 2024-08-23 22:48:51

不,事实并非如此。

DateTime 在内部将其值存储为包含自 01/01/0001 起 100 纳秒的 long 值。

由于GetHashCode返回一个32位整数,因此它不可能完全唯一。

这是 DateTime 的实现:

public override int GetHashCode() {
    Int64 ticks = InternalTicks;
    return unchecked((int)ticks) ^ (int)(ticks >> 32); 
}

No, it doesn't.

DateTime internally stores its value as a long containing 100-nanosecond untis since 01/01/0001.

Since GetHashCode returns a 32-bit integer, it's impossible for it to be completely unqiue.

Here is DateTime's implementation:

public override int GetHashCode() {
    Int64 ticks = InternalTicks;
    return unchecked((int)ticks) ^ (int)(ticks >> 32); 
}
时光倒影 2024-08-23 22:48:51

DateTime.Now 返回一个 DateTime 值,我确信它有自己的哈希码实现。这是实现。

public override int GetHashCode()
{
    long internalTicks = this.InternalTicks;
    return (((int) internalTicks) ^ ((int) (internalTicks >> 0x20)));
}

DateTime.Now returns a DateTime value which I am sure has its own implementation of the hash code. Here is the implementation.

public override int GetHashCode()
{
    long internalTicks = this.InternalTicks;
    return (((int) internalTicks) ^ ((int) (internalTicks >> 0x20)));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文