DateTime.Now 是否有自己的 GetHashCode 实现来提供唯一的哈希值?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,依赖于
DateTime
的GetHashCode
的特定实现是错误的。那是对你隐藏的东西。依赖隐藏的细节是一种糟糕的代码味道;他们随时可能改变你的行为并破坏你的代码。其次,事实证明
DateTime
内部存储了一个 64 位整数DateTime.Ticks
测量自纪元(0001 年 1 月 1 日午夜)以来 100 纳秒单位的数量。因此,DateTime
实例至少需要 64 位信息。但哈希码是 32 位整数,因此哈希码不可能是唯一的(无法将 64 位空间映射到 32 位空间而不发生冲突)。明确地说,您可以查看
DateTime.GetHashCode
的源代码:如您所见,它执行了一些“折叠”操作,将
InternalTicks
压缩为 32 位整数。一般来说,不要依赖哈希码是唯一的。输入空间通常大于被散列到的空间(所有32位整数的空间)。
如果您绝对必须有一个唯一的键来表示
DateTime
对象,请使用DateTime.ToBinary
。这将为您提供一个唯一的 64 位整数,可用于重构DateTime
(使用DateTime.FromBinary
)。First, it would be a mistake to rely on the particular implementation of
GetHashCode
forDateTime
. 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 integerDateTime.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
: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, useDateTime.ToBinary
. This will provide you with a 64-bit integer that is unique and can be used to reconstitute theDateTime
(useDateTime.FromBinary
).不,事实并非如此。
DateTime
在内部将其值存储为包含自 01/01/0001 起 100 纳秒的long
值。由于
GetHashCode
返回一个32位整数,因此它不可能完全唯一。这是
DateTime
的实现:No, it doesn't.
DateTime
internally stores its value as along
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:DateTime.Now 返回一个 DateTime 值,我确信它有自己的哈希码实现。这是实现。
DateTime.Now returns a DateTime value which I am sure has its own implementation of the hash code. Here is the implementation.