Unique identifiers are essential in programming for identifying records, ensuring uniqueness across distributed systems, and maintaining consistency. The two popular formats for unique identifiers in modern applications are UUID (also known as GUID in C#) and ULID. In this blog, we will explore what these formats are, how to use them in C#, and their pros and cons. We'll also discuss real-world use cases and share tips for developers and testers.
What is a UUID (GUID)?
UUID stands for Universally Unique Identifier, and in C#, it's commonly referred to as a GUID (Globally Unique Identifier). It's a 128-bit identifier designed to be unique across systems and time.
Structure of UUID
A UUID is typically represented as a 36-character string (32 hexadecimal digits and 4 hyphens). It consists of five groups separated by hyphens, such as:
550e8400-e29b-41d4-a716-446655440000
How to Use UUID in C#
C# provides a built-in Guid
struct to generate and work with UUIDs:
Generate a UUID
Guid newGuid = Guid.NewGuid();
Console.WriteLine(newGuid.ToString());
Parse a UUID
string uuidString = "550e8400-e29b-41d4-a716-446655440000";
Guid parsedGuid = Guid.Parse(uuidString);
Validate UUID
bool isValidGuid = Guid.TryParse(uuidString, out Guid result);
Console.WriteLine(isValidGuid ? "Valid GUID" : "Invalid GUID");
Pros and Cons of UUID

What is a ULID?
ULID stands for Universally Unique Lexicographically Sortable Identifier. It is a newer alternative to UUID with added benefits like being sortable and more readable.
Structure of ULID
A ULID is a 26-character string encoded in base32. For example:
01H5HZ8X9E7EY2XKZCW2FQX16B
It consists of:
- 48 bits for timestamp (sortable by time)
- 80 bits for randomness
How to Use ULID in C#
ULID is not natively supported in C#, but you can use libraries like ulid.net.
Install ULID Library
Install the package via NuGet:
dotnet add package NUlid
Generate a ULID
using NUlid;
Ulid newUlid = Ulid.NewUlid();
Console.WriteLine(newUlid.ToString());
Parse a ULID
string ulidString = "01H5HZ8X9E7EY2XKZCW2FQX16B";
Ulid parsedUlid = Ulid.Parse(ulidString);
Pros and Cons of ULID over UUID

Real-World Example
Problem
Imagine you're building a distributed e-commerce system where:
- Each order needs a unique ID for tracking.
- Orders must be sortable by creation time.
- You want IDs that are space-efficient and avoid collisions.
Solution
- Use UUID if you prioritize widespread adoption and a standardized approach.
- Use ULID if you need IDs sortable by time and more compact storage.
Example: Generating IDs for order tracking in C# using ULID.
using NUlid;
public class Order
{
public Ulid OrderId { get; private set; }
public DateTime CreatedAt { get; private set; }
public Order()
{
this.OrderId = Ulid.NewUlid();
this.CreatedAt = DateTime.UtcNow;
}
}
Tips for Developers and Testers
Developers
- Choose Based on Needs: Use UUIDs for compatibility and ULIDs for sortability.
- Optimize Storage: Store UUIDs as
BINARY(16)
or ULIDs asbase32
strings for efficiency. - Standard Libraries: Leverage libraries like
Guid
for UUID andNUlid
for ULID to save time.
Testers
- Test Edge Cases: Validate uniqueness across distributed systems.
- Sortability Tests: For ULIDs, ensure sorting behavior works correctly.
- Performance Metrics: Test generation speed under high-load scenarios.
Final Thoughts
Both UUID and ULID are powerful tools for unique identification, each with its strengths and weaknesses. If compatibility and standardization are key, go with UUID. For compactness and sorting, ULID is a better choice. Understanding their use cases will help you choose the right one for your project.
If you enjoy the content I create and would like to show your appreciation, you can buy me a coffee!