Osy#the first language built for agents
Agents firstAgentic appsWorkflowsDurable Execution — built inSecurityTestingThe editorThe UI modelOne program

Reference / Function

Guid.Empty and Guid.NewGuid

Guid.Empty → Guid (all-zero constant) · Guid.NewGuid() → Guid (fresh v4)

The C# Guid statics. `Guid.Empty` is the all-zero Guid constant, spelled without parens; `Guid.NewGuid()` mints a fresh unique Guid. Guid.Empty pushes down into SQL predicates; Guid.NewGuid() is in-memory only.

stable1 example compiled by CIfunctionguidstdlib

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 parentheses

The 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#

Related

String interpolation & format specifiers

Build a string from literal text and embedded expressions with $"…{expr}…". A hole may carry a .NET format specifier…