How does C# generate GUIDs?
Solution 1
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???
A .Net System.Guid
is 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.
Solution 2
There's a really good article on Raymond Chen's blog that describes how GUIDs are generated, and in particular why a substring of a guid is not guaranteed to be unique.
Basically, a a GUID is generated using a combination of:
- 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)
However, this is only 1 particular algorithm used for generating GUIDs (although I believe it's the one used by the .NET framework), and is not the one used by the .NET framework.
Solution 3
The algorithm is documented here as Globally unique identifier
Solution 4
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".
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.
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.
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).
Solution 5
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.
Developer404
Independent, motivated individual with good problem solving and software development skills
Updated on July 09, 2022Comments
-
Developer404 almost 2 years
How are GUIDs generated in C#?
-
cjk over 14 yearsA GUID is NOT guaranteed to be unique, it is just very unlikely to generate two the same.
-
Dave Mateer over 14 yearsYou don't need (indeed, can't use) the new keyword if using Guid.NewGuid()
-
Paul B. over 10 years.NET uses Windows' algorithm which creates a random number (version 4 GUID) starting with Windows 2000: stackoverflow.com/a/2757969/1059776
-
Ognyan Dimitrov almost 10 yearsThis answer is wrong because it answers to "What is GUID?" but the real question is "How does C# generate GUIDs?". Therefore the answer provided by @Justin is the right answer.
-
jpmc26 over 9 yearsThat article discusses several different algorithms for generating GUIDs. Which one is the one .NET actually uses?
-
Dave Mateer over 9 years@OgnyanDimitrov - The original question was pretty ambiguous because of broken English. It sounded like the request had to do with the string representation of the GUID. You can look at the history of the question. It's arguable that the meaning of the original question was changed, given the edits that were made and the fact that this answer was accepted by the asker. I'll add the original question to my answer, in case that's the question someone is coming to this page with.
-
Ognyan Dimitrov over 9 yearsPlease, do not be offended. I do not write this to blame or ridicule. You can edit your answer now not because of anything else but to improve the quality of it even though the question was vague in the first place. The next guy that comes across from a search result will get the better answer. I am sure that you are experience and knowledgeable and I did not mean to belittle your answer.
-
Mark Amery almost 7 years-1; this answer basically torpedoes its entire premise with the link at the end of the final sentence and admits to not being an answer to the question asked.
-
Mark Amery almost 7 yearsThe question asked about GUID generation in C#, and this answer is irrelevant to that; -1.
-
Mark Amery almost 7 years-1; per the Wikipedia article you link to, there are 5 different standardised UUID-generation algorithms. "see the linked Wikipedia article" thus doesn't answer the question of how C# in particular generates UUIDs.
-
Mark Amery almost 7 years-1; this answer is not true of GUIDs generated with C#'s
Guid.NewGuid
on Windows, as noted in stackoverflow.com/a/2757969/1709587. Indeed, there are good privacy reasons for all languages/OSes/frameworks to avoid the GUID-generation algorithm you describe here (or at least not use it as a default), as the convict David L. Smith could explain to you. -
Urasquirrel about 3 yearsThis is one of the best answers I've found all day.