When you want to identify class instances or data records, you can’t use things like passports. Yet it’s just as important to keep track of object identities as it is for people.

One simple way to keep track of things is to just use a number. Even a simple unsigned integer should give you over 4 billion numbers. Just start at 0 and increment each time you need a new one. You can even start at one just in case you want to reserve zero to mean something special. There’s lots of problems with this approach especially when you need to coordinate unique identifiers between multiple threads or even multiple computers.

A GUID solves all these issues in a simple way that not only works now but can scale later when needed. GUID stands for globally unique identifier. and you might sometimes also see them referred to as a UUID which stands for universally unique identifier.

GUIDs are huge. They use 128 bits and an article on Wikipedia gives an idea of just how many values are possible. It says that in order for the probability of a single duplicate GUID value to reach 50%, then every person on earth would need to own 600 million GUIDs. What this means for you is that you no longer have to worry about how to ensure that your identifiers are unique. Just generate a GUID value and use it. When you need another one, you don’t increment the first one. That’s not how GUIDs work. Instead, you generate a whole new GUID each time you need one.

The only things you can meaningfully do with GUIDs are generate them and test them for equality. What I mean is that there’s no concept of one GUID being less than or greater than another GUID. They’re either equal or not. They’re not incremented like a simple number. And they don’t look like a number either. When you see a GUID displayed, it’s usually formatted as 32 hexadecimal numbers separated into 5 uneven groups with dashes between each group. Sometimes, the formatted value will appear inside curly braces, and sometimes inside quotation marks, sometimes any hexadecimal characters will be in uppercase and sometimes in lowercase. Just remember that this is just for display purposes. A GUID is still just 128 binary bits. Here’s what a GUID looks like when displayed:



What's on your mind?
On a scale of 0 to 10, how likely are you to refer us to friends?