DEV Community 👩‍💻👨‍💻

Joe Enos
Joe Enos

Posted on

.NET GUID Formats

GUIDs (aka UUIDs) are 128-bit moderately random numbers that make great identifiers when you're looking for something a little more random than incrementing integers. In .NET, you get several built-in format strings.

// 00000000-0000-0000-0000-000000000000
Guid.NewGuid().ToString()    // Default

// 00000000-0000-0000-0000-000000000000
Guid.NewGuid().ToString("D") // "D"efault

// 00000000000000000000000000000000
Guid.NewGuid().ToString("N") // "N"o formatting

// {00000000-0000-0000-0000-000000000000}
Guid.NewGuid().ToString("B") // "B"races

// (00000000-0000-0000-0000-000000000000)
Guid.NewGuid().ToString("P") // "P"arentheses

// {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}
Guid.NewGuid().ToString("X") // "X"-tremely confusing
Enter fullscreen mode Exit fullscreen mode

I generally use the "N" format, since it doesn't waste any space, and GUIDs are used by machines, not people, so readability isn't super important. However, the other ones are there if you need them.

I've never been able to find any explanation for what the "X" format is for. If you know, please comment - it's driven me nuts for decades now.

Top comments (1)

Collapse
 
ahmedabdelrazek profile image
Ahmed Mohamed Abdel-Razek

"X"-tremely confusing lamo

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.