C# 如何生成 GUID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1888254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:39:59  来源:igfitidea点击:

How does C# generate GUIDs?

c#.netwinformsguid

提问by Developer404

How are GUIDs generated in C#?

在 C# 中如何生成 GUID?

采纳答案by Dave Mateer

Original question:

原问题:

How the Guid is generating it's identifier?? How will be it's output if I use the following code Guid g = Guid.NewGuid();

Whether the output will be the combination of numbers and lettters or the numbers alone will be there???

Guid 如何生成它的标识符?如果我使用以下代码 Guid g = Guid.NewGuid(); 它将如何输出?

输出是数字和字母的组合还是单独的数字????

A .Net System.Guidis just a 128-bit integer (16 bytes). Numbers and letters have nothing to do with it. You can use the ToString() method to see various "human-readable" versions of a Guid, which include numbers 0-9 and letters A-F (representing hex values), but that's all up to you how you want to output it.

.NetSystem.Guid只是一个 128 位整数(16 字节)。数字和字母与它无关。您可以使用 ToString() 方法查看 Guid 的各种“人类可读”版本,其中包括数字 0-9 和字母 AF(表示十六进制值),但这完全取决于您希望如何输出它。

回答by David

The algorithm is documented here as Globally unique identifier

该算法在此处记录为全局唯一标识符

回答by jason

Details on Wikipediaand MSDN.

维基百科MSDN上的详细信息。

In short, a GUID is a random 128-bit integer, but we format them using hex digits so that they are easily readable (this is the 8-4-4-4-12 format that you see). As far as how its generated, see the linked Wikipedia article.

简而言之,GUID 是一个随机的 128 位整数,但我们使用十六进制数字格式化它们,以便它们易于阅读(这是您看到的 8-4-4-4-12 格式)。至于它是如何生成的,请参阅链接的维基百科文章。

回答by t0mm13b

A Guid is a unique number that has billions of permutations and is made up of a mixture of hexadecimal and numbers in groups. And it is generated based on the different factors, such as time, windows version, system resources such as hard disks, motherboards, devices and so on. A Guid is guaranteed to be unique.(Thanks ck!) I have not seen an instance where you do

Guid 是一个独特的数字,它有数十亿个排列,由十六进制和数字组合而成。它是根据不同的因素生成的,如时间、Windows 版本、系统资源如硬盘、主板、设备等。Guid 保证是唯一的。(谢谢 ck!)我还没有看到你这样做的实例

Guid g = new Guid.NewGuid();
Guid g = Guid.NewGuid(); /* Thanks Dave! */

where gwill have the same value in successive runs.

whereg将在连续运行中具有相同的值。

It is more commonly used for mutexes, an example would be to create a single instance of an application and by using a mutex with a guid guarantees the lifetime of the instance of the application.

它更常用于互斥锁,例如创建应用程序的单个实例,并通过使用带有 guid 的互斥锁来保证应用程序实例的生命周期。

Even, in some instances, it is used in the database but the method of using it is frowned upon.

甚至,在某些情况下,它在数据库中使用,但使用它的方法不受欢迎。

回答by rmalayter

There are also other forms of GUID besides "random 16 bytes", as mentioned in the RFC. Some Microsoft products (SQL Server for instance) can optionally generate these "sequential GUIDs" which are based on a combination of "MAC address of first network card in the system + ever-increasing counter based on system time".

除了 RFC 中提到的“随机 16 字节”之外,还有其他形式的 GUID。某些 Microsoft 产品(例如 SQL Server)可以选择性地生成这些“顺序 GUID”,它们基于“系统中第一个网卡的 MAC 地址 + 基于系统时间的不断增加的计数器”的组合。

These "sequential GUIDs" have the nice property of always appending new records to the "end" of a database table when used as a database primary key with a clustered index. This helps prevent database index fragmentation and page splits.

当用作具有聚集索引的数据库主键时,这些“顺序 GUID”具有始终将新记录附加到数据库表的“末尾”的良好属性。这有助于防止数据库索引碎片和页面拆分。

If random GUIDs are used as database primary keys with clustered indexes, new records will be inserted randomly in the "middle" of a table from a physical allocation standpoint, which leads to index fragmentation and partially-full database pages over time.

如果将随机 GUID 用作具有聚集索引的数据库主键,则从物理分配的角度来看,新记录将随机插入表的“中间”,这会导致索引碎片和部分满的数据库页面随着时间的推移而出现。

Using sequential GUIDs still allows you to generate GUIDs independently on multiple systems and be confident there will not be any collisions (a property that you do not get using sequential integers as primary keys without allocating "ranges" or different seeds and increments to each system, which is an administrative nightmare in large distributed applications).

使用顺序 GUID 仍然允许您在多个系统上独立生成 GUID,并确信不会发生任何冲突(您无法使用顺序整数作为主键而不为每个系统分配“范围”或不同的种子和增量的属性,这是大型分布式应用程序中的管理噩梦)。

回答by Justin

There is a really good article herethat describes how GUIDs are generated, and in particular why a substring of a guid is not guarenteed to be unique.

有一个很好的文章在这里描述的GUID是如何产生的,尤其是为什么一个GUID的一个子不保证的广告是唯一的。

Basiclly a GUID is generated using a combination of

基本上,GUID 是使用以下组合生成的

  • The MAC address of the machine used to generate the GUID(so GUIDs generated on different machines are unique unless MAC addresses are re-used)
  • Timestamp(so GUIDs generated at different times on the same machine are unique)
  • Extra "emergency uniquifier bits"(these are used to ensure that GUIDs generated at nearly exactly the same time on the same machine are unique)
  • An identifier for the algorithm(so that GUIDs generated with a different algorithm are unique)
  • 用于生成 GUID 的机器的 MAC 地址(因此在不同机器上生成的 GUID是唯一的,除非重新使用 MAC 地址)
  • 时间戳(因此在同一台机器上不同时间生成的 GUID 是唯一的)
  • 额外的“紧急唯一符位”(这些用于确保在同一台机器上几乎完全相同的时间生成的 GUID 是唯一的)
  • 算法的标识符(以便使用不同算法生成的 GUID 是唯一的)

However, this is only 1 particular algorithm used for generating GUIDs (although I believe its the one used by the .Net framework), and is not the one used by the .Net framework

但是,这只是用于生成 GUID 的 1 种特定算法(尽管我相信它是 .Net 框架使用的算法),而不是.Net 框架使用的算法

回答by Prince Prasad

The bits of the GUID break down like this:

GUID 的位分解如下:

  • 60 bits of timestamp
  • 48 bits of computer identifier
  • 14 bits of uniquifier
  • 6 bits are fixed
  • 60 位时间戳
  • 48位计算机标识符
  • 14 位唯一标识符
  • 6位固定

Total of 128 bits.

总共 128 位。

回答by Michael Balmes

It depends. For .NET Core in Unix GUIDs, are generated by creating a random number of 128 bits and and doing a couple bit wise operations. In .NET Core for Windows and .NET framework it makes a remote procedure call to the Windows function UuidCreate(so it's completely up to your Windows version on how they are generated). For Unix and recent versions of Windows, you'll notice that there is one hex digit that is always a 4. That is because it the version number for the Uuid 4, which just means they are generated with random bytes. GUIDs used to be generated with things like the timestamp and MAC address, but that became an attack vector because it told end users information about the system and helped them predict future GUIDs easier.

这取决于。对于 Unix GUID 中的 .NET Core,通过创建 128 位的随机数并执行一些位操作来生成。在适用于 Windows 的 .NET Core 和 .NET 框架中,它对 Windows 函数UuidCreate进行远程过程调用(因此,它们的生成方式完全取决于您的 Windows 版本)。对于 Unix 和最新版本的 Windows,您会注意到有一个十六进制数字始终是 4。这是因为它是 Uuid 4 的版本号,这意味着它们是用随机字节生成的。GUID 过去是使用时间戳和 MAC 地址之类的东西生成的,但这变成了一种攻击媒介,因为它告诉最终用户有关系统的信息并帮助他们更轻松地预测未来的 GUID。