可以使用 DateTime.Now 的 Ticks 生成重复的唯一标识符吗? [英] Can using Ticks of DateTime.Now can generate duplicates unique identifiers?

查看:49
本文介绍了可以使用 DateTime.Now 的 Ticks 生成重复的唯一标识符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码是否有可能生成相同的身份,

Is there a possibility for this code to generate the same identity,

for (int i = 0; i < 20; i++)
    {
        string TransID = DateTime.Now.Ticks.ToString();
    }

导致这种情况发生的考虑因素是什么?

What are the considerations that make this happen?

推荐答案

是的,此代码可以生成相同的标识符.

无论刻度精度如何,如果在 Windows 和/或 Bios 中更改系统日期,它都可以这样做.

Regardless of the tick precision, it can do so if the system date is changed in Windows and/or the Bios.

如果它是由不同时区的计算机运行的,或者如果您旅行.

后一种情况意味着即使在本地和未连接的应用程序中,您也绝对不应使用此类代码.

如果你个人和本地申请需要身份证,而且你从不旅行,这个代码可以做你想做的,但不建议使用这种糟糕的模式.

If you need ID for a personnal and local application, and you never travel, this code does what you want, but it is not recommended to use such bad pattern.

正如大家所说,没有错,你应该使用 Guid.New() 这实际上是最安全的标准,或者 int 或 long 自动增量(用于本地或由服务器管理) 在某些情况下获得更好的性能.

As everyone says, without being wrong, you should use Guid.New() which is actually the safest standard, or an int or a long auto-increment (for local or managed by a server) for a better performance in some cases.

https://docs.microsoft.com/dotnet/api/system.datetime.now

https://en.wikipedia.org/wiki/Unique_identifier

https://en.wikipedia.org/wiki/Universally_unique_identifier

GUID 有多贵强制转换和比较 vs 字符串比较

这篇关于可以使用 DateTime.Now 的 Ticks 生成重复的唯一标识符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆