.NET Short Unique Identifier
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In .NET, a “short unique identifier” usually means one of two things: a compact encoding of a Guid, or a different ID scheme entirely such as a random token, snowflake-style ID, or database-generated value. The correct choice depends on whether you need global uniqueness, sortability, URL-friendliness, or very short length.
The first design question is not “how do I make it shorter?” It is “what uniqueness guarantees do I actually need?”
Option 1: Encode a Guid More Compactly
A Guid is 128 bits whether you print it as 36 characters or not. If you want the same uniqueness with a shorter string, encode the bytes with URL-safe Base64.
This often gives a 22-character string instead of the usual 36-character GUID text. The underlying uniqueness is unchanged because you are only changing the representation.
Decode It Back When Needed
If you need reversible storage or debugging, add the reverse operation.
That makes the format practical when you want short IDs in URLs but still want to recover the original Guid later.
Option 2: Use a Different Identifier Scheme
Sometimes a 22-character short GUID is still too long. If you truly need fewer characters, you are usually no longer talking about “the same as a GUID but shorter.” You are trading off something:
- collision probability
- decentralization
- sortability
- predictability
- reversibility
For example, a random 8-character token is easy to generate, but it does not give the same collision safety as a full 128-bit identifier.
This may be fine for invitation codes or temporary references, but not for high-scale globally unique primary keys.
Pick the Identifier Based on the Use Case
A reasonable rule of thumb:
- use
Guidor short-encodedGuidfor general distributed uniqueness - use database integers when local monotonic IDs are enough
- use ULID-style or similar schemes when ordering by creation time matters
- use short random tokens when human readability matters more than strong uniqueness guarantees
The wrong design is usually choosing a short random string because it “looks nicer,” then discovering collisions later.
Common Pitfalls
- Assuming a shorter string automatically preserves GUID-level uniqueness.
- Using
Guid.NewGuid().ToString().Substring(...), which destroys uniqueness guarantees arbitrarily. - Forgetting URL-safe replacements when the short ID will appear in routes or query strings.
- Choosing a very short random token without estimating collision risk at your expected scale.
- Mixing multiple ID schemes in one system without documenting what each one means.
Summary
- In
.NET, the safest short unique ID is often a URL-safe Base64 encoding of aGuid. - That makes the text shorter without changing the underlying 128-bit uniqueness.
- If you need fewer characters than that, you are choosing a different ID scheme with different tradeoffs.
- Never truncate GUID strings blindly and assume the result is still safe.
- Pick identifier length and format based on collision tolerance, ordering needs, and where the ID will be used.
Related reading
- .NET Simplest way to send POST with data and read response
- .NET Standard vs .NET Core
- .NET String.Format() to add commas in thousands place for a number
- .NET String.Format to add commas in thousands place for a number
- .net System.Web.Mail vs System.Net.Mail
- .NET TimeZoneInfo from Olson time zone
- .NET Which Exception to Throw When a Required Configuration Setting is Missing?
- .NET WPF Remember window size between sessions

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.