C# Reference ยท Types and Literals
The Guid struct
Reference for System.Guid in C#: NewGuid, CreateVersion7, Parse and TryParse, the format strings N D B P X, comparison and database tips.
Guid (globally unique identifier, also called UUID) is a 128-bit value used to identify things without a central counter: database rows, files, messages, correlation IDs. The chance of two random GUIDs colliding is negligible, so different machines can create IDs independently.
The usual text form is 32 hex digits in groups: 3f2504e0-4f89-11d3-9a0c-0305e82c3301. Guid.NewGuid() creates a random (version 4) value. Since .NET 9, Guid.CreateVersion7() creates a time-ordered value that sorts roughly by creation time, which is friendlier to database indexes.
Guid id = Guid.NewGuid(); Console.WriteLine(id.ToString().Length);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public readonly struct Guid : IComparable<Guid>, IEquatable<Guid>, ISpanFormattable, IParsable<Guid>
| Member | Description |
|---|---|
Guid.NewGuid() |
Random version 4 GUID from a cryptographic source. |
Guid.CreateVersion7() / CreateVersion7(DateTimeOffset) |
Time-ordered version 7 GUID (.NET 9+). |
Guid.Empty |
All zeros. |
Parse, TryParse, ParseExact, TryParseExact |
From text (and from ReadOnlySpan<char>). |
ToString(format) |
D (default, hyphens), N (no hyphens), B (braces), P (parentheses), X (hex array). |
ToByteArray(), new Guid(byte[]) |
16-byte form. |
Version, Variant |
Which UUID version and variant (.NET 9+). |
Errors: Parse throws FormatException for bad text; the Guid(byte[]) constructor throws ArgumentException unless the array has exactly 16 bytes.
Usage notes
- Never rely on the text output of
NewGuid()in tests: it differs on every run. Parse a known value instead. - Random GUIDs make poor clustered keys. Inserting in random order causes page splits in many databases. Version 7 values (or a database-specific sequential GUID) keep inserts near the end of the index.
- Not a secret. A GUID is an identifier, not a password or an access token. Use a purpose-built random token for security.
- Byte order.
ToByteArray()stores the first three groups little-endian, which differs from the RFC byte order seen in text. UseToByteArray(isBigEndian: true)when interoperating with other systems. - Comparison.
==andEqualscompare the 128 bits.CompareToand sorting use an ordering that is not the same as sorting by the hyphenated text. - Default value.
default(Guid)equalsGuid.Empty, so uninitialized fields and missing database values look the same. - Size. 16 bytes as a value, 36 characters as text. Store the binary form when space matters.
- Dictionary keys.
Guidworks well as a key, and its hash is well distributed.