Hexalith.Commons.UniqueIds 2.13.0

dotnet add package Hexalith.Commons.UniqueIds --version 2.13.0
                    
NuGet\Install-Package Hexalith.Commons.UniqueIds -Version 2.13.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hexalith.Commons.UniqueIds" Version="2.13.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hexalith.Commons.UniqueIds" Version="2.13.0" />
                    
Directory.Packages.props
<PackageReference Include="Hexalith.Commons.UniqueIds" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Hexalith.Commons.UniqueIds --version 2.13.0
                    
#r "nuget: Hexalith.Commons.UniqueIds, 2.13.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Hexalith.Commons.UniqueIds@2.13.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Hexalith.Commons.UniqueIds&version=2.13.0
                    
Install as a Cake Addin
#tool nuget:?package=Hexalith.Commons.UniqueIds&version=2.13.0
                    
Install as a Cake Tool

Hexalith.Commons.UniqueIds

Three ID strategies for .NET — DateTime, Base64URL, and ULID — pick the right one in 30 seconds.

NuGet


Overview

Hexalith.Commons.UniqueIds provides three unique identifier strategies via the static UniqueIdHelper class:

Method Format Length Sortable Distributed-Safe Thread-Safe Best For
GenerateDateTimeId() yyyyMMddHHmmssfff 17 chars Yes (chronological) No (single machine) Yes (locked) Log entries, file names
GenerateUniqueStringId() Base64URL GUID 22 chars No Yes Yes (stateless) Legacy keys, session tokens
GenerateSortableUniqueStringId() Crockford Base32 ULID 26 chars Yes (chronological) Yes Yes (monotonic) Event sourcing, DDD aggregates

Installation

dotnet add package Hexalith.Commons.UniqueIds

Quick Start

using Hexalith.Commons.UniqueIds;

// Sortable + distributed (event sourcing, DDD)
string ulidId = UniqueIdHelper.GenerateSortableUniqueStringId();
// "01HYX7QS3NP8M4KQJR5A7CVWKM" — 26-char ULID

// Distributed (legacy keys, session tokens)
string base64Id = UniqueIdHelper.GenerateUniqueStringId();
// "gZOW2EgVrEq5SBJLegYcVA" — 22-char Base64URL

// Human-readable (single machine, logs)
string dateId = UniqueIdHelper.GenerateDateTimeId();
// "20260314143052789" — 17-char timestamp

Method Reference

GenerateSortableUniqueStringId()

Generates a 26-character ULID — sortable, distributed-safe, ideal for event sourcing.

string id = UniqueIdHelper.GenerateSortableUniqueStringId();
// "01HYX7QS3NP8M4KQJR5A7CVWKM"

// ULIDs sort chronologically as plain strings
string[] ids = Enumerable.Range(0, 5)
    .Select(_ => UniqueIdHelper.GenerateSortableUniqueStringId())
    .ToArray();
// ids are already in creation order — no OrderBy needed

GenerateUniqueStringId()

Generates a 22-character Base64URL string from a GUID. Distributed-safe but not sortable.

string id = UniqueIdHelper.GenerateUniqueStringId();
// "gZOW2EgVrEq5SBJLegYcVA"

// URL-safe — use directly in REST routes
// GET /api/orders/gZOW2EgVrEq5SBJLegYcVA

GenerateDateTimeId()

Generates a 17-character UTC timestamp ID. Human-readable and sortable, but single-machine only.

string id = UniqueIdHelper.GenerateDateTimeId();
// "20260314143052789"

// Format breakdown: yyyyMMddHHmmssfff
// Thread-safe: same-millisecond calls auto-increment

Conversion Utilities

ExtractTimestamp(string ulid)

Need to know when an entity was created? Extract the embedded UTC timestamp from any ULID.

string ulid = UniqueIdHelper.GenerateSortableUniqueStringId();
DateTimeOffset created = UniqueIdHelper.ExtractTimestamp(ulid);
// Returns the exact UTC time the ULID was generated

ToGuid(string ulid)

Your ERP only accepts GUIDs? Convert your internal ULID while preserving the 128-bit identity.

string ulid = UniqueIdHelper.GenerateSortableUniqueStringId();
Guid guid = UniqueIdHelper.ToGuid(ulid);
// Use guid for external systems that require System.Guid

Note: ToGuid preserves identity but NOT lexicographic sort order. Two ULIDs that sort correctly as strings may not sort the same way as Guids due to byte-order differences.

ToSortableUniqueId(Guid value)

Converting back from a Guid to a ULID string? Round-trip is lossless for Guids originally derived from ULIDs.

// Round-trip: ULID → Guid → ULID
string original = UniqueIdHelper.GenerateSortableUniqueStringId();
Guid guid = UniqueIdHelper.ToGuid(original);
string restored = UniqueIdHelper.ToSortableUniqueId(guid);
// restored == original (case-insensitive)

Note: Converting a non-ULID Guid (e.g., Guid.NewGuid()) produces a valid ULID string, but its embedded timestamp is meaningless.


Decision Guide

Use Case Recommended Method
Event sourcing / DDD aggregates GenerateSortableUniqueStringId() — sortable + distributed
Distributed keys / session tokens GenerateUniqueStringId() — compact + GUID-backed
Log entries / file names GenerateDateTimeId() — human-readable timestamps
Migrating Base64URL to ULID Generate ULID for new records; keep old Base64URL IDs unchanged
Interop with Guid-only systems ToGuid() / ToSortableUniqueId() — lossless round-trip

Combined Usage

public class AuditLog
{
    // ULID: sortable, distributed-safe event ID
    public string EventId { get; } = UniqueIdHelper.GenerateSortableUniqueStringId();

    // Base64URL: globally unique correlation for tracing
    public string CorrelationId { get; set; }

    public string Action { get; set; }
    public string UserId { get; set; }
}

var log = new AuditLog
{
    CorrelationId = UniqueIdHelper.GenerateUniqueStringId(),
    Action = "OrderPlaced",
    UserId = "user-42"
};

Thread Safety

All three methods are fully thread-safe:

var ids = new ConcurrentBag<string>();

Parallel.For(0, 10_000, _ =>
{
    ids.Add(UniqueIdHelper.GenerateSortableUniqueStringId());
});

// All 10,000 IDs are unique and in monotonic order within each millisecond
Debug.Assert(ids.Distinct().Count() == 10_000);

License

MIT License — See LICENSE for details.


Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on Hexalith.Commons.UniqueIds:

Package Downloads
Hexalith.Commons.Metadatas

Hexalith is a set of libraries to build an application with micro-service architecture.

Hexalith.KeyValueStorages

Hexalith KeyValueStorages utilities and helpers

Hexalith.KeyValueStorages.Files

Hexalith KeyValueStorages utilities and helpers

Hexalith.KeyValueStorages.DaprComponents

Hexalith KeyValueStorages utilities and helpers

Hexalith.Applications.Abstractions

Hexalith is a set of libraries to build an application with micro-service architecture.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.13.0 157 3/14/2026
2.12.0 136 3/14/2026
2.11.0 133 3/14/2026
2.10.0 132 3/14/2026
2.9.0 130 3/14/2026
2.8.0 108 1/10/2026
2.7.0 294 12/23/2025
2.6.1 197 12/23/2025
2.6.0 523 12/22/2025
2.5.0 229 12/21/2025
2.4.1 179 12/21/2025
2.4.0 269 12/21/2025
2.3.0 166 12/21/2025
2.2.2 164 12/6/2025
2.2.1 386 11/30/2025
2.2.0 307 11/13/2025
2.1.9 215 10/14/2025
2.1.8 208 10/13/2025
2.1.7 234 9/20/2025
2.1.6 158 8/10/2025
Loading failed