UUID
C#
Programming
.NET
Code Generation

How can I generate UUID in C

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In C#, the normal way to generate a UUID is to create a Guid. The .NET term is GUID, but it represents the same 128-bit identifier concept that many systems call a UUID. Most applications should use Guid.NewGuid() and stop there unless they have a very specific storage or ordering requirement.

The Standard Solution: Guid.NewGuid()

The simplest working example is:

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        Guid id = Guid.NewGuid();
8        Console.WriteLine(id);
9    }
10}

A typical output looks like this:

text
7d444840-9dc0-11d1-b245-5ffdce74fad2

That value is generated by the runtime and is designed to be practically unique for application use. For most APIs, database records, correlation IDs, and one-off identifiers, this is the right approach.

UUID Versus GUID In .NET

You will see both terms used interchangeably:

  • 'UUID is the broader industry term'
  • 'GUID is the Microsoft and .NET type name'

In code, you work with System.Guid, not a separate Uuid type.

csharp
Guid id = Guid.NewGuid();

If someone asks "how do I generate a UUID in C#," this is almost always the answer they need.

Common String Formats

A Guid can be formatted in different ways depending on where it will be stored or displayed.

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        Guid id = Guid.NewGuid();
8
9        Console.WriteLine(id.ToString("D"));
10        Console.WriteLine(id.ToString("N"));
11        Console.WriteLine(id.ToString("B"));
12    }
13}

These formats produce:

  • '"D": the default hyphenated form'
  • '"N": 32 hexadecimal digits with no hyphens'
  • '"B": the hyphenated form wrapped in braces'

Choose the format based on the consumer. Database columns, JSON payloads, and URLs often prefer the default "D" format because it is readable and common.

Parsing A UUID Back Into A Guid

If you receive a UUID as a string, parse it instead of treating it as arbitrary text.

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        string input = "7d444840-9dc0-11d1-b245-5ffdce74fad2";
8
9        if (Guid.TryParse(input, out Guid id))
10        {
11            Console.WriteLine($"Parsed: {id}");
12        }
13        else
14        {
15            Console.WriteLine("Invalid GUID");
16        }
17    }
18}

Guid.TryParse is safer than Guid.Parse when input may be invalid, because it avoids exceptions for normal validation failures.

When Guid.NewGuid() Is The Right Tool

Use it when you need:

  • identifiers generated on the client or server without coordination
  • unique IDs for records, messages, or request tracing
  • stable external identifiers that should not reveal database row counts

It is a poor fit when you need:

  • sortable IDs by creation time
  • cryptographic secrets or tokens
  • compact numeric IDs for user-facing URLs

Those are different problems. A GUID is about uniqueness, not secrecy or ordering.

Database Considerations

GUIDs work well as identifiers, but they have tradeoffs in indexed databases. Random GUID values can fragment clustered indexes because new rows are inserted all over the key space instead of near the end.

That does not mean "never use GUIDs." It means you should understand the storage consequences:

  • for external identifiers, GUIDs are often a good choice
  • for clustered primary keys, some systems prefer sequential identifiers
  • many teams store both an internal numeric key and an external GUID

That is an architectural decision, not a reason to replace Guid.NewGuid() with a homemade generator.

Do Not Use Random For UUIDs

A common mistake is trying to build a UUID manually with Random or string concatenation. That gives you worse uniqueness guarantees and often invalid formatting.

Bad idea:

csharp
1using System;
2
3string fakeId = new Random().Next().ToString();
4Console.WriteLine(fakeId);

That is not a UUID. It is just a random integer string. Use the standard library type that already solves the problem correctly.

Common Pitfalls

  • Searching for a separate UUID library when System.Guid already does the job.
  • Using Random to generate identifiers and calling them UUIDs.
  • Assuming GUIDs are suitable as security tokens. They are identifiers, not secrets.
  • Forgetting to validate strings with Guid.TryParse when input comes from users or external systems.
  • Choosing GUIDs for a database key without thinking about indexing and sort behavior.

Summary

  • In C#, generate a UUID with Guid.NewGuid().
  • 'GUID is the .NET name for the same kind of 128-bit identifier commonly called a UUID.'
  • Use ToString formats when you need a specific textual representation.
  • Parse inbound values with Guid.TryParse.
  • Do not replace the built-in GUID generator with a custom Random-based solution.

Course illustration
Course illustration

All Rights Reserved.