How to Generate a UUID in C#
The simplest way to generate a UUID in C# is Guid.NewGuid(), which is built directly into the .NET base class library and uses a cryptographically secure random number generator to produce a 128-bit identifier with virtually zero chance of collision. No package install, no configuration — just call the method. The ruby uuid page includes Sinatra and Hanami UUID patterns alongside full Rails UUID configuration.
using System;
Guid myGuid = Guid.NewGuid();
Console.WriteLine(myGuid);
// Output: 550e8400-e29b-41d4-a716-446655440000
Explanation
using System;brings theGuidstruct into scope — it ships with the .NET base class library, so no NuGet package is required.Guid.NewGuid()generates a random UUID (version 4) using a cryptographically secure random source under the hood.Console.WriteLine(myGuid)callsGuid'sToString()override automatically, producing the standard 36-character hyphenated format.Guidis a value type (struct), so passing it around your codebase never requires heap allocation or null checks.
Convert a String to a UUID in C#
Sometimes you receive a UUID as a plain string — from a form field, a JSON payload, or a route parameter — and need it back as a proper Guid. C# gives you two ways to do that: Guid.Parse() and Guid.TryParse(). See the rust uuid page for Cargo.toml setup, type-safe UUID generation, and Diesel ORM integration.
using System;
string input = "550e8400-e29b-41d4-a716-446655440000";
// Parse - throws FormatException if the string is invalid
Guid parsed = Guid.Parse(input);
// TryParse - returns bool, no exception, safe for untrusted input
if (Guid.TryParse(input, out Guid result))
{
Console.WriteLine($"Valid GUID: {result}");
}
else
{
Console.WriteLine("Invalid GUID format");
}
Explanation
Guid.Parse(input)converts a string straight into aGuid, but throws aFormatExceptionif the string isn't a valid 32-character hex UUID.Guid.TryParse(input, out Guid result)returns aboolinstead of throwing, making it the safer choice whenever the string comes from user input, an API request, or a config file.- Both methods accept UUIDs with or without hyphens, in uppercase or lowercase.
- Two
Guidvalues compare equal with==when they represent the same identifier, regardless of how each was originally parsed or generated.
Other Options for Generating UUIDs in C#
.NET 9 added native support for UUID version 7 through Guid.CreateVersion7(), producing a time-sortable, RFC 9562-compliant identifier that's particularly useful as a database primary key. Combine it with a format specifier when you need a shorter or differently styled string.
using System;
// .NET 9+: time-sortable UUID v7, ideal for database primary keys
Guid guidV7 = Guid.CreateVersion7();
Console.WriteLine(guidV7);
// Format without hyphens
string compact = guidV7.ToString("N");
// Format wrapped in braces
string braced = guidV7.ToString("B");
Explanation
Guid.CreateVersion7()(new in .NET 9) encodes the current Unix millisecond timestamp in the most significant bits, so IDs sort chronologically — this can meaningfully improve clustered index performance over random v4 GUIDs.- RFC 9562 formalizes UUID v7 as the recommended choice for database primary keys and other index-friendly identifiers.
guid.ToString("N")strips the hyphens, producing a compact 32-character hex string.guid.ToString("B")wraps the standard format in curly braces, matching the style Windows GUIDs traditionally use in registry keys and COM interfaces.