DevToolsForYou

UUID Generator in C# / .NET — Code Examples

UUID Generator in C# / .NETUse the online tool →

UUIDs (Universally Unique Identifiers) are 128-bit identifiers used as database primary keys, session tokens, and idempotency keys. UUID v4 (random) is the safe default; UUID v7 (time-ordered) is preferred for database primary keys.

C# uses Guid for UUIDs. Guid.NewGuid() generates a v4-equivalent random GUID. .NET 9 adds Guid.CreateVersion7() for time-ordered v7.

C# / .NET
using System;

// UUID v4 — random
Guid id = Guid.NewGuid();
Console.WriteLine(id); // 550e8400-e29b-41d4-a716-446655440000

// String format variants
Console.WriteLine(id.ToString("D")); // with dashes (default)
Console.WriteLine(id.ToString("N")); // no dashes, lowercase hex
Console.WriteLine(id.ToString("B")); // {braces with dashes}
Console.WriteLine(id.ToString("P")); // (parentheses with dashes)

// Parse a UUID / GUID string (throws FormatException on invalid input)
Guid parsed = Guid.Parse("550e8400-e29b-41d4-a716-446655440000");

// Safe parse (returns bool instead of throwing)
if (Guid.TryParse("550e8400-e29b-41d4-a716-446655440000", out Guid result))
    Console.WriteLine(result);

// UUID v7 — time-ordered (.NET 9+)
// Guid id7 = Guid.CreateVersion7();
// Console.WriteLine(id7);
Notes & gotchas
  • Guid.NewGuid() is internally equivalent to UUID v4 — it uses a CSPRNG for random bytes.
  • Guid.CreateVersion7() was added in .NET 9 and generates a time-ordered UUID suitable for database primary keys.
  • Use Guid.TryParse instead of Guid.Parse when validating user input to avoid exceptions.
Try it in your browser

Need to uuid generator without writing code? The UUID Generator runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open UUID Generator
UUID Generator in other languages