Code guide

Generate UUIDs (GUIDs) in C#

Generate GUIDs in C# with Guid.NewGuid, parse and validate them, and create UUID v7 on .NET 9 and later.

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();

FAQ

Generate UUIDs (GUIDs) in C# questions

Are GUID and UUID the same in C#?

Yes. .NET calls the 128-bit identifier a Guid, which is the same format as a UUID.

How do I generate UUID v7 in C#?

On .NET 9 and later, use Guid.CreateVersion7(). On older runtimes, use a library.

Should I use Guid.Parse or Guid.TryParse?

Use TryParse for user input so invalid values return false instead of throwing.

Generate UUID / GUID in C# - .NET Examples