What is a GUID in C#
Guid.NewGuid() returns a version 4 GUID generated by the cryptographic provider. It is available in every .NET version and is the normal choice for entity keys, message ids and cache keys. The type is a struct, so a Guid field costs 16 bytes with no allocation.
.NET 9 added Guid.CreateVersion7(), which follows RFC 9562 and starts with a Unix millisecond timestamp — the variant you want when the column is clustered or indexed, because new rows land next to each other instead of scattering. The same release added an overload that takes a DateTimeOffset, so you can generate a v7 for a specific moment. GUIDs are written in five shapes: D (hyphenated), N (32 digits), B (braces), P (parentheses) and X (with 0x prefixes) — pick the one the consuming system expects.
How to
- Call
Guid.NewGuid()for a random v4 value. - On .NET 9+, call
Guid.CreateVersion7()for a time-ordered value. - Use
ToString("B")orToString("N")to match the format the other side needs — the formatter above does the same for a whole list.
Use cases
- Entity Framework Core keys: assign the value in the model so the key exists before the INSERT.
- SQL Server UNIQUEIDENTIFIER columns, where
NEWID()gives v4 andNEWSEQUENTIALID()a sequential value. - Distributed systems and message ids, where each node must be able to mint an identifier without coordinating.
Compare GUID in C# with other formats
| Option | When to use |
|---|---|
Guid.NewGuid() | v4, random, available everywhere. |
Guid.CreateVersion7() | RFC 9562, time-ordered, .NET 9 and later. |
NEWSEQUENTIALID() | SQL Server-side sequential value; unique per table index but not a legal v7. |
Entity Framework Core | Client-side generation keeps the graph consistent before SaveChanges. |
Code examples
Random v4
using System;
Guid id = Guid.NewGuid();
Console.WriteLine(id); // 550e8400-e29b-41d4-a716-446655440000
Console.WriteLine(id.ToString("N")); // 550e8400e29b41d4a716446655440000
Console.WriteLine(id.ToString("B")); // {550e8400-e29b-41d4-a716-446655440000}
Time-ordered v7 (.NET 9+)
using System;
Guid v7 = Guid.CreateVersion7();
Console.WriteLine(v7); // 019535d9-3df7-79fb-b466-fa907fa17f9e
Console.WriteLine(v7.Version); // 7
// A specific moment, for backfills
Guid historical = Guid.CreateVersion7(
new DateTimeOffset(2025, 1, 1, 0, 0, 0, TimeSpan.Zero));
Format specifiers
Guid g = Guid.NewGuid();
g.ToString("N"); // 32 digits, no hyphens
g.ToString("D"); // 8-4-4-4-12, the default
g.ToString("B"); // braces, the .NET config-file shape
g.ToString("P"); // parentheses
g.ToString("X"); // hexadecimal: 0x...
Parsing safely
if (Guid.TryParse(input, out Guid parsed))
{
// well-formed and legal
}
else
{
// reject the input instead of throwing
Frequently asked questions
Is a GUID the same as a UUID?
Yes. GUID is Microsoft's name for the same 128-bit structure; a .NET Guid is interchangeable with a UUID byte for byte. The only practical difference is terminology and the fact that the framework also offers braces and parentheses notation.
Should I use v4 or v7 for SQL Server keys?
If the GUID is the clustered key, v7 (or NEWSEQUENTIALID()) keeps the index compact — random v4 values cause page splits because every insert lands in a different part of the tree. If the key is non-clustered, a random v4 is perfectly fine.
How much space does a GUID cost?
16 bytes in the database when stored in a native uniqueidentifier column, or 36 characters as text. Storing it as text costs more than twice as much and compares slower, so use the native type where you can.