Summary#
Guid.Empty is the all-zero Guid (00000000-0000-0000-0000-000000000000) — a constant, spelled without
parens exactly like C#'s static property. Guid.NewGuid() mints a fresh, unique Guid on each call. Both
are typed Guid.
Signature#
Guid.Empty // the all-zero constant (a property — no parens)
Guid.NewGuid() // a fresh, unique Guid (a factory call)Description#
Guid.Empty is a deterministic constant. It is the idiomatic sentinel for an unset Guid — e.g. guarding a
reference before use:
if (ownerId != Guid.Empty) { … }Because it is a constant, Guid.Empty pushes down into SQL — it is usable inside a query predicate
(Widget.Where(w => w.Id != Guid.Empty)), where it renders as the zero-uuid literal.
Guid.NewGuid() is non-deterministic (a new value every call), so — like the crypto/random generators
— it runs in memory only and has no SQL push-down form; calling it inside a query predicate is an error.
Guid.NewGuid() is the faithful C# spelling and the single way to mint a Guid (it replaced the earlier
Security.NewGuid()).
Guid.Empty or Guid.NewGuid()? — the parentheses are enforced#
Swapping the two spellings is an error, in both directions, exactly as it is in C#:
Guid.Empty() // error — `Guid.Empty` is a property, not a method
Guid.NewGuid // error — `Guid.NewGuid` is a method; call it with parenthesesThe distinction is not decoration: Guid.Empty is a value that is always the same one, and Guid.NewGuid()
mints a new value every time it runs. The parens are how a reader tells those apart at a glance, so the
compiler holds you to them. The same rule covers every parenless member of the standard library —
TimeSpan.Zero, DateTime.UtcNow, DateTime.Today, DateTimeOffset.UtcNow and the DurableClock reads.
Examples#
Guid PickOwner(Guid requested) {
if (requested != Guid.Empty) {
return requested; // caller supplied one
}
return Guid.NewGuid(); // otherwise mint a fresh owner id
}See also#
- String interpolation & format specifiers — Guids stringify through the same value coercion