C# / .NET

Generate UUID (GUID) in C#

Use the built-in System.Guid — available since .NET 1.0, no NuGet package needed. Covers Guid.NewGuid(), EF Core, and ASP.NET Core.

Need a UUID right now? Generate one instantly in your browser.

Open UUID v4 Generator →

The Quick Answer

C# calls UUIDs "GUIDs". The System.Guid struct is built into .NET — no NuGet package needed.

C# — generate GUID / UUID
using System;

Guid id = Guid.NewGuid();
Console.WriteLine(id);
// → "f47ac10b-58cc-4372-a567-0e02b2c3d479"

// As a string
string idStr = Guid.NewGuid().ToString();           // "f47ac10b-58cc-4372-a567-0e02b2c3d479"
string noHyphens = Guid.NewGuid().ToString("N");    // "f47ac10b58cc4372a5670e02b2c3d479"
string braces = Guid.NewGuid().ToString("B");       // "{f47ac10b-58cc-4372-a567-0e02b2c3d479}"
string parens = Guid.NewGuid().ToString("P");       // "(f47ac10b-58cc-4372-a567-0e02b2c3d479)"

// Parse an existing GUID string
Guid parsed = Guid.Parse("f47ac10b-58cc-4372-a567-0e02b2c3d479");
bool isValid = Guid.TryParse(someString, out Guid result);

Entity Framework Core — GUID Primary Key

EF Core — entity with GUID primary key
using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;

public class Product
{
    public Guid Id { get; set; } = Guid.NewGuid();  // client-generated

    [Required]
    public string Name { get; set; } = string.Empty;
}

// In DbContext — SQL Server: use newsequentialid() for index-friendly GUIDs
protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<Product>()
        .Property(p => p.Id)
        .HasDefaultValueSql("newsequentialid()");  // SQL Server only
}

ASP.NET Core — Route Parameter

ASP.NET Core — GUID route parameter
using Microsoft.AspNetCore.Mvc;
using System;

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    [HttpGet("{id:guid}")]
    public IActionResult GetProduct(Guid id)
    {
        // :guid constraint validates automatically — returns 404 for invalid GUIDs
        var product = _service.Find(id);
        return product is null ? NotFound() : Ok(product);
    }

    [HttpPost]
    public IActionResult Create(CreateProductDto dto)
    {
        var product = new Product { Id = Guid.NewGuid(), Name = dto.Name };
        _service.Add(product);
        return CreatedAtAction(nameof(GetProduct), new { id = product.Id }, product);
    }
}

Sequential GUIDs for Better Database Performance

Random GUIDs cause B-tree index fragmentation in SQL Server and other databases because rows are inserted at random positions. Use newsequentialid() (SQL Server) or a library like MassTransit.NewId to generate sequential GUIDs in C#.

C# — MassTransit NewId (sequential GUID)
// dotnet add package MassTransit.NewId
using MassTransit;

Guid sequentialId = NewId.NextSequentialGuid();
// Sorts sequentially while still being globally unique

Frequently Asked Questions

What is the difference between UUID and GUID in C#?

They are the same thing. GUID (Globally Unique Identifier) is Microsoft's name for a UUID. C# uses the System.Guid type, which is a 128-bit UUID fully compatible with the RFC 4122 standard.

Is Guid.NewGuid() thread-safe in C#?

Yes. Guid.NewGuid() is thread-safe and can be called concurrently from multiple threads without synchronization.

How do I use a GUID as a primary key in Entity Framework Core?

Set the entity's Id property type to Guid. EF Core will automatically generate a new GUID when a new entity is inserted. For SQL Server, configure .HasDefaultValueSql("newsequentialid()") in OnModelCreating for index-friendly sequential GUIDs.

What format string should I use with Guid.ToString() in C#?

Use "D" (default) for the standard hyphenated format, "N" for 32 hex digits without hyphens, "B" for braces format, and "P" for parentheses format. For most interoperability (e.g. sending to a REST API or storing in a database), the default hyphenated "D" format is correct.

Related Tools & Guides