Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/uuids/csharp$ _

Generate UUIDs (GUIDs) in C#

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: C#

What: Generate UUIDs (called GUIDs in .NET) using the <code>System.Guid</code> struct. Built into .NET Framework, .NET Core, and .NET 5+.

Try it: Use our interactive Uuids generator or integrate this code into your C# application.

Generate UUIDs (called GUIDs in .NET) using the System.Guid struct. Built into .NET Framework, .NET Core, and .NET 5+. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C++ , Ruby and Go or use our interactive web generator.

C# Code Example

using System;

class UuidExample
{
    static void Main()
    {
        // Generate new GUID (UUID v4)
        Guid guid = Guid.NewGuid();
        Console.WriteLine(guid);  // Example: 3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f

        // Format variations
        Console.WriteLine(guid.ToString());           // Default: 3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f
        Console.WriteLine(guid.ToString("N"));        // No dashes: 3d6f9e647d8a4c3e8f2b1a4e5c6d7e8f
        Console.WriteLine(guid.ToString("D"));        // With dashes (default)
        Console.WriteLine(guid.ToString("B"));        // Braces: {3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f}
        Console.WriteLine(guid.ToString("P"));        // Parentheses: (3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f)

        // Parse GUID from string
        string guidString = "3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f";
        if (Guid.TryParse(guidString, out Guid parsed))
        {
            Console.WriteLine($"Parsed: {parsed}");
        }

        // Generate multiple GUIDs
        var guids = new List<Guid>();
        for (int i = 0; i < 5; i++)
        {
            guids.Add(Guid.NewGuid());
        }

        // Empty GUID
        Guid empty = Guid.Empty;  // 00000000-0000-0000-0000-000000000000
        Console.WriteLine($"Empty: {empty}");
    }
}

[EXPLANATION]

Guid.NewGuid() generates UUID v4 using cryptographically secure random number generation. The Guid struct provides multiple formatting options. In C#, UUIDs are called GUIDs (Globally Unique Identifiers).

Expected Output

3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f
3d6f9e647d8a4c3e8f2b1a4e5c6d7e8f
{3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f}
(3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f)
00000000-0000-0000-0000-000000000000

Common Use Cases

  • ASP.NET Core entity IDs
  • Entity Framework primary keys
  • Azure resource identifiers
  • WPF/WinForms unique controls
  • Xamarin mobile app IDs
  • SignalR connection IDs

Important Notes

  • Guid is a value type (struct), not reference type
  • Use Guid.Empty for null/default GUID checks
  • TryParse() is safer than Parse()
  • SQL Server stores GUIDs as UNIQUEIDENTIFIER type

Try Our Interactive Generator

Don't want to write code? Use our free web-based Uuids generator with instant results.

TRY UUIDS GENERATOR →