Back to Home

C# GUID Parse and Format Reference

GUIDs commonly arrive as strings from APIs, config files, or databases. Use Guid.Parse() when the input is trusted and Guid.TryParse() when it may be invalid.

Parse vs TryParse

C# Example Code
Guid parsed = Guid.Parse("3f2504e0-4f89-11d3-9a0c-0305e82c3301");
Console.WriteLine(parsed);

if (Guid.TryParse("not-a-guid", out Guid result))
{
    Console.WriteLine(result);
}
else
{
    Console.WriteLine("Invalid GUID");
}

Format specifiers

C# Example Code
Guid id = Guid.NewGuid();

Console.WriteLine(id.ToString("D")); // 3f2504e0-4f89-11d3-9a0c-0305e82c3301
Console.WriteLine(id.ToString("N")); // 3f2504e04f8911d39a0c0305e82c3301
Console.WriteLine(id.ToString("B")); // {3f2504e0-4f89-11d3-9a0c-0305e82c3301}
Console.WriteLine(id.ToString("P")); // (3f2504e0-4f89-11d3-9a0c-0305e82c3301)
Console.WriteLine(id.ToString("X")); // {0x3f2504e0,0x4f89,0x11d3,{0x9a,0x0c,0x03,0x05,0xe8,0x2c,0x33,0x01}}

Validation and normalization helpers

C# Example Code
string Normalize(Guid value) => value.ToString("D");

bool IsValidGuid(string input) => Guid.TryParse(input, out _);

FAQ

Which format should I use?

Use D for human-readable display and N for compact storage or URLs.

Do I need to parse before comparing?

Yes. Comparing strings can be case-sensitive and is less robust than comparing Guid values directly.

Take It Further

The C# Player's Guide book cover

The community's favorite C# guide — teaches every concept you just read about with clarity.

The C# Player's Guide — 5th Edition · RB Whitaker

Get it on Amazon →

As an Amazon Associate I earn from qualifying purchases.