无需数据库即可生成唯一编号

发布于 2024-10-07 03:53:12 字数 71 浏览 0 评论 0原文

如何在没有数据库的情况下在 C# 中生成唯一编号?

(最多 17 位数字)。

编辑:仅限数字。

How do I generate a unique number in c# without the database?

(max 17 digits).

EDIT: digits only.

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

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

发布评论

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

评论(2

著墨染雨君画夕 2024-10-14 03:53:12

您可以查看 Twitter 的开源 Snowflake 项目 来获取灵感,该项目解决了类似的问题。它是用 Scala 编写的,但简化版本用 C# 编码非常简单。

前提是获取当前时间戳,并将其按位左移,留下右侧位可用于简单的序列号,每次时间戳递增时,该序列号都会重置为 0。

通过调整位长度并改变时间戳的粒度(例如,决定使用秒还是毫秒),您应该能够非常简单地生成可以容纳 17 位数字的内容。

You could take a look at Twitter's open source Snowflake project for inspiration, which solves a similar problem. It's written in Scala, but a simplified version is quite simple to code in C#.

The premise is to get the current timestamp, and bitwise shift it left, leaving the right-hand bits available for a simple sequence number, which is reset to 0 every time the timestamp increments.

By playing around with the bit lengths, and altering the granularity of your timestamp (e.g. decide whether to use seconds or milliseconds), you should be able to produce something which can fit into 17 digits quite simply.

青巷忧颜 2024-10-14 03:53:12

您可以使用 GUID,GUID(全局唯一标识符)是一个 128 位整数,可用于唯一标识某些内容。

System.Guid.NewGuid()

You can use GUID, A GUID (Globally unique identifier) is a 128-bit integer that can be used to uniquely identify something.

System.Guid.NewGuid()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文