In .NET, UUIDs are represented by the Guid type. Guid.NewGuid() covers version 4, and .NET 9 added native UUID v7 generation.
Key takeaways
- Guid.NewGuid() creates a version 4 identifier.
- Guid.TryParse validates input without throwing.
- .NET 9 adds Guid.CreateVersion7() for time-ordered GUIDs.
For implementation context, continue with GUID Generator, UUID vs GUID, and UUID v7. These pages cover the closest generator, comparison, validation or storage decisions without repeating this guide.
Generate and validate a GUID
NewGuid returns a random version 4 value. Use TryParse for safe validation of user input.
Guid id = Guid.NewGuid(); // version 4
string text = id.ToString();
if (Guid.TryParse(input, out Guid parsed))
{
// parsed now holds a valid GUID
}UUID v7 on modern .NET
.NET 9 introduced native time-ordered GUIDs, which index better in databases than random values.
// .NET 9 and later
Guid v7 = Guid.CreateVersion7();