How to Convert String to GUID and GUID to String in C#

Convert a string to a GUID in C# with Guid.Parse or Guid.TryParse, and convert a GUID back to a string with ToString() when you need standard or compact formatting.

Recommendation: prefer Guid.TryParse for untrusted input, and use guid.ToString("D") unless you have a specific integration format requirement.

How to convert a string to GUID in C#

Use Guid.Parse() when the input is guaranteed to be valid, or Guid.TryParse() when you want to avoid exceptions.

using System;

string input = "550e8400-e29b-41d4-a716-446655440000";

Guid parsed = Guid.Parse(input);
Console.WriteLine(parsed);

if (Guid.TryParse(input, out Guid safeParsed))
{
    Console.WriteLine($"Parsed safely: {safeParsed}");
}

How to convert GUID to string in C#

Guid.ToString() supports multiple format specifiers depending on what another system expects.

using System;

Guid id = Guid.Parse("550e8400-e29b-41d4-a716-446655440000");

Console.WriteLine(id.ToString());      // D: 550e8400-e29b-41d4-a716-446655440000
Console.WriteLine(id.ToString("N"));   // N: 550e8400e29b41d4a716446655440000
Console.WriteLine(id.ToString("B"));   // B: {550e8400-e29b-41d4-a716-446655440000}
Console.WriteLine(id.ToString("P"));   // P: (550e8400-e29b-41d4-a716-446655440000)
Console.WriteLine(id.ToString("X"));   // X: {0x550e8400,0xe29b,0x41d4,{0xa7,0x16,0x44,0x66,0x55,0x44,0x00,0x00}}

Practical notes

  • The default ToString() format is D, which includes hyphens.
  • N is useful when another system expects 32 hexadecimal characters without dashes.
  • TryParse is the safer choice for API input, config values, and user-entered data.
  • Parsing and formatting do not change the underlying 128-bit GUID value.

Frequently Asked Questions

Use Guid.Parse() only when invalid input should be treated as a bug. Use Guid.TryParse() for external input so you can handle invalid values without throwing an exception.

The default is format D: 32 hexadecimal digits separated by hyphens, such as 550e8400-e29b-41d4-a716-446655440000.

Learn more

These articles expand on related concepts, formats and practical considerations.

By using this site, you agree to our Privacy Policy and Terms of Service. You are not permitted to use the GUIDs (also known as UUIDs) generated by this site or use any other content, services and information available if you do not agree to these terms.
Disclaimer: All information is provided for general educational and technical reference only. While we aim to keep the content accurate, current and aligned with published standards, no guarantees are made regarding completeness, correctness or suitability for any specific use case.
GUID specifications, best practices, security guidance, database behavior and ecosystem conventions (including cloud platforms and identifier formats) may change over time or differ by implementation. Examples, recommendations and comparisons are illustrative and may not apply universally.
This content should not be considered legal, security, compliance or architectural advice. Before making critical design, security or production decisions, always consult the latest official standards and vendor-specific documentation.
Always evaluate behavior in your own environment.
Standards Compliance: The GUIDs generated by this site conform to RFC 4122 and RFC 9562 specifications whenever possible, using cryptographically secure random number generation.