Developer Documentation
C#
Updated September 19, 2026

Generate and Work with GUIDs in C#

Generate, parse, format, and validate GUID/UUID values in C#.

A GUID is Microsoft terminology for a 128-bit globally unique identifier. In practice, the canonical textual form is compatible with UUID formatting, although platform APIs may expose additional GUID-specific formats.

Quick Start#

The following example uses the standard C# API or a widely used package for this task.

Quick start#

C#
1using System;23Guid id = Guid.NewGuid();4Console.WriteLine(id);5// Example: d2719d7e-4050-4b1c-a7e0-4dbd1b7c8705

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Formatting and parsing#

C#
1Guid id = Guid.NewGuid();2Console.WriteLine(id.ToString("D")); // 32 digits + hyphens3Console.WriteLine(id.ToString("N")); // 32 digits4Console.WriteLine(id.ToString("B")); // {xxxxxxxx-....}56if (Guid.TryParse(input, out Guid parsed)) {7    Console.WriteLine(parsed);8}

Important Notes#

Prefer Guid.TryParse for untrusted input. Guid.NewGuid() produces a random version-4 UUID on current .NET platforms.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement GUID in other languages: