Developer Documentation
C#
Updated September 18, 2026

Generate a UUID in C#

Generate, format, parse, and validate UUIDs and GUIDs in C# and .NET with native Guid methods and .NET 9 UUIDv7 support.

In C# and the .NET runtime, UUIDs are represented by the System.Guid value type (struct). While historically Microsoft used the term GUID (Globally Unique Identifier), .NET's Guid struct implements RFC 4122/9562 standard 128-bit UUIDs and is binary-compatible with UUIDs generated on Linux, macOS, and web platforms.

Quick Start#

Generate a standard random UUID v4 in C# using Guid.NewGuid():

C#
1using System;23// Generate random UUIDv44Guid id = Guid.NewGuid();56Console.WriteLine(id.ToString());7// Output: "d8f6d2f3-11b2-4cf3-96b6-98dc124e402b"

.NET 9 UUIDv7 Support#

Starting with .NET 9, Microsoft introduced native support for time-ordered UUIDv7 via Guid.CreateVersion7():

C#
1// Native in .NET 9+2Guid timeOrderedId = Guid.CreateVersion7();34Console.WriteLine(timeOrderedId);5// Leading 48 bits encode the current Unix epoch millisecond timestamp

UUIDv7 provides optimal performance for SQL Server, PostgreSQL, and SQLite primary keys by drastically reducing B-tree page splits.

Formatting Formats & Specifiers#

The Guid.ToString(string format) method supports standardized format specifiers:

C#
1Guid id = Guid.NewGuid();23// "D": 32 digits separated by hyphens (canonical RFC 4122/9562 format, default)4string standard = id.ToString("D");5// "d8f6d2f3-11b2-4cf3-96b6-98dc124e402b"67// "N": 32 digits without hyphens8string digitsOnly = id.ToString("N");9// "d8f6d2f311b24cf396b698dc124e402b"1011// "B": Hyphenated enclosed in braces12string braces = id.ToString("B");13// "{d8f6d2f3-11b2-4cf3-96b6-98dc124e402b}"1415// "P": Hyphenated enclosed in parentheses16string parens = id.ToString("P");17// "(d8f6d2f3-11b2-4cf3-96b6-98dc124e402b)"

Parsing and Validation#

Use Guid.TryParse for fast, allocation-free validation that handles all standard string formats without throwing exceptions:

C#
1string input = "d8f6d2f3-11b2-4cf3-96b6-98dc124e402b";23if (Guid.TryParse(input, out Guid parsedGuid))4{5    Console.WriteLine($"Valid Guid: {parsedGuid}");67    // Check if it's the empty/nil GUID (00000000-0000-0000-0000-000000000000)8    if (parsedGuid == Guid.Empty)9    {10        Console.WriteLine("Warning: Nil UUID received");11    }12}13else14{15    Console.WriteLine("Invalid GUID string");16}

Converting to Byte Arrays & Endianness Caution#

When converting a Guid to bytes in .NET, be aware of Microsoft's historical mixed-endian internal byte layout:

C#
1Guid id = Guid.NewGuid();23// Warning: id.ToByteArray() returns Microsoft mixed-endian byte order4byte[] dotNetBytes = id.ToByteArray();56// In .NET 8+, use TryWriteBytes with BigEndian parameter for true RFC 4122/9562 byte order:7Span<byte> rfcBytes = stackalloc byte[];8id.TryWriteBytes(rfcBytes, bigEndian: true, out _);

Best Practices#

  1. Use Guid.TryParse instead of Guid.Parse: Avoids costly exception handling when validating user inputs.
  2. Check for Guid.Empty: Ensure required foreign keys or entity IDs are not default uninitialized structs.
  3. Use Guid.CreateVersion7() in .NET 9+: Replace sequential GUID generators (such as RT.Comb or NEWSEQUENTIALID()) with standard UUIDv7.

References#