NUlid 1.7.3
dotnet add package NUlid --version 1.7.3
NuGet\Install-Package NUlid -Version 1.7.3
<PackageReference Include="NUlid" Version="1.7.3" />
<PackageVersion Include="NUlid" Version="1.7.3" />
<PackageReference Include="NUlid" />
paket add NUlid --version 1.7.3
#r "nuget: NUlid, 1.7.3"
#addin nuget:?package=NUlid&version=1.7.3
#tool nuget:?package=NUlid&version=1.7.3
NUlid
A .Net ULID implementation
Universally Unique Lexicographically Sortable Identifier
A GUID/UUID can be suboptimal for many use-cases because:
- It isn't the most character efficient way of encoding 128 bits
- It provides no other information than randomness
A ULID however:
- Is compatible with UUID/GUID's
- 1.21e+24 unique ULIDs per millisecond (1,208,925,819,614,629,174,706,176 to be exact)
- Lexicographically sortable
- Canonically encoded as a 26 character string, as opposed to the 36 character UUID
- Uses Crockford's base32 for better efficiency and readability (5 bits per character)
- Case insensitive
- No special characters (URL safe)
Installation
PM> Install-Package NUlid
Or simply use the Nuget package manager GUI in Visual Studio.
Usage
Creating a ULID:
// Create a ULID
var myulid = Ulid.NewUlid();
// Print ULID
Console.WriteLine(myulid);
Output:
01ASB2XFCZJY7WHZ2FNRTMQJCT
Parsing a ULID:
// Parse ULID:
var myulid = Ulid.Parse("01ASB2XFCZJY7WHZ2FNRTMQJCT");
// Print time-part of ULID:
Console.WriteLine(myulid.Time);
Output:
4-8-2016 15:31:59 +00:00
You can also convert from/to GUID/UUID's, get the byte-representation of a ULID, create a ULID with specific timestamp and you can even specify an IUlidRng
to use for generating the randomness (by default NUlid uses the CSUlidRng
but a SimpleUlidRng
is also provided, as well as a MonotonicUlidRng
). The ULID is implemented as a struct
with (operator) overloads for (in)equality, comparison etc. built-in and is, generally, very much like .Net's native Guid
struct. An extensive helpfile is provided in the Nuget package and the testsuite also serves as a (simple) demonstration of NUlid's features.
Specification
Below is the current specification of ULID as implemented in this repository.
01AN4Z07BY 79KA1307SR9X4MV3
|----------| |----------------|
Timestamp Randomness
10 chars 16 chars
48bits 80bits
base32 base32
Components
Timestamp
- 48 bit integer
- UNIX-time in milliseconds
- Won't run out of space till the year 10895 AD (this .Net specific Ulid implementation limits this to DateTimeOffset.MaxValue).
Randomness
- 80 (Whenever possible: Cryptographically secure) Random bits
As of v1.4.0 monotonic ULID's are supported (see below).
Monotonicity
When generating a ULID within the same millisecond, it is possible to provide some guarantees regarding sort order (with some caveats). When you use the MonotonicUlidRng
and a newly generated ULID in the same millisecond is detected, the random component is incremented by 1 bit in the least significant bit position (with carrying). For example:
// Create monotonic rng
var rng = new MonotonicRng();
// Create ULIDs, assume that these calls occur within the same millisecond:
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MCX
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MCY
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MCZ
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MD0
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MD1
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MD2
By default the most significant bit of the random part is set to zero; this ensures you can generate enough ULID's after the initial one before causing an overflow. Some implementations simply pick a random value for the random part and increment this value, however, there's a (very small) chance that this random part is close to the overflow value. If you then happen to generate a lot of ULID's within the same millisecond there is a risk the you hit the overflow. By our method we ensure there's enough 'room' for new values before 'running out of values' (overflowing). It is, with some effort, even possible to 'resume counting' from any given ULID.
Encoding
Crockford's Base32 is used as shown. This alphabet excludes the letters I, L, O, and U to avoid confusion and abuse.
0123456789ABCDEFGHJKMNPQRSTVWXYZ
Binary Layout and Byte Order
The components are encoded as 16 octets. Each component is encoded with the Most Significant Byte first (network byte order).
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 32_bit_uint_time_high |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 16_bit_uint_time_low | 16_bit_uint_random |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 32_bit_uint_random |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| 32_bit_uint_random |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
String Representation
ttttttttttrrrrrrrrrrrrrrrr
Where:
t
is Timestamp
r
is Randomness
Prior Art
Based on / inspired by alizain/ulid.
Performance
Below measurements are based on an Intel(R) Core(TM) i9-10900X CPU @ 3.70Ghz:
BenchmarkDotNet v0.14.0, Windows 11 (10.0.26100.4061)
Intel Core i9-10900X CPU 3.70GHz, 1 CPU, 20 logical and 10 physical cores
.NET SDK 9.0.204
[Host] : .NET 9.0.5 (9.0.525.21509), X64 RyuJIT AVX-512F+CD+BW+DQ+VL
DefaultJob : .NET 9.0.5 (9.0.525.21509), X64 RyuJIT AVX-512F+CD+BW+DQ+VL
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|------------------------------------- |-----------:|----------:|----------:|-------:|----------:|
| Guid.NewGuid() | 61.045 ns | 0.4488 ns | 0.3978 ns | - | - |
| Ulid.NewUlid(SimpleUlidRng) | 35.331 ns | 0.2170 ns | 0.1694 ns | - | - |
| Ulid.NewUlid(CSUlidRng) | 104.516 ns | 0.5099 ns | 0.4258 ns | - | - |
| Ulid.NewUlid(SimpleMonotonicUlidRng) | 51.985 ns | 0.3772 ns | 0.3344 ns | - | - |
| Ulid.NewUlid(CSMonotonicUlidRng) | 52.000 ns | 0.1184 ns | 0.1050 ns | - | - |
| Guid.Parse(string) | 100.885 ns | 1.2808 ns | 1.1354 ns | 0.0095 | 96 B |
| Ulid.Parse(string) | 199.476 ns | 3.2495 ns | 3.0396 ns | 0.0181 | 184 B |
| Guid.ToString() | 76.089 ns | 0.8230 ns | 0.6426 ns | 0.0095 | 96 B |
| Ulid.ToString() | 131.441 ns | 0.5919 ns | 0.4943 ns | 0.0079 | 80 B |
| 'new Guid(byte[])' | 9.341 ns | 0.1644 ns | 0.1538 ns | 0.0040 | 40 B |
| 'new Ulid(byte[])' | 11.045 ns | 0.1987 ns | 0.1951 ns | 0.0040 | 40 B |
| Guid.ToByteArray() | 65.470 ns | 0.1393 ns | 0.1163 ns | 0.0039 | 40 B |
| Ulid.ToByteArray() | 111.239 ns | 0.9540 ns | 0.7966 ns | 0.0038 | 40 B |
| Ulid.ToGuid() | 106.292 ns | 0.2446 ns | 0.2043 ns | - | - |
| 'new Ulid(Guid)' | 65.200 ns | 0.2050 ns | 0.1712 ns | - | - |
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Memory (>= 4.6.3)
-
net6.0
- No dependencies.
NuGet packages (12)
Showing the top 5 NuGet packages that depend on NUlid:
Package | Downloads |
---|---|
EachShow.Util
通用基础功能 |
|
Farsica.Framework
Asp.net Core Application Framework |
|
NidRpc
Library for internal RPC on FR22 SmartReader |
|
LeadSoft.Common.GlobalDomain
LeadSoft® Common Global Domain and crossproject DTOs and Classes. |
|
CA.Blocks.DataAccess.Extensions.Translators.NUlid
Package Description |
GitHub repositories (5)
Showing the top 5 popular GitHub repositories that depend on NUlid:
Repository | Stars |
---|---|
1Remote/1Remote
One Remote Access Manager to Rule Them All
|
|
Cysharp/Ulid
Fast .NET C# Implementation of ULID for .NET and Unity.
|
|
platformplatform/PlatformPlatform
A platform designed for building enterprise-grade, multi-tenant products using Azure, .NET, React, TypeScript, Infrastructure as Code, etc.
|
|
FormCms/FormCms
Open-source headless CMS built with ASP.NET Core (C#) and React, featuring REST APIs, GraphQL, and a GrapesJS page designer.
|
|
valentinajemuovic/banking-kata-dotnet
Banking Kata (.NET)
|
Version | Downloads | Last updated |
---|---|---|
1.7.3 | 0 | 5/16/2025 |
1.7.2 | 274,165 | 3/7/2024 |
1.7.1 | 585,723 | 8/30/2022 |
1.7.0 | 173,889 | 1/14/2022 |
1.5.0 | 454,969 | 7/9/2020 |
1.4.1 | 433,418 | 7/16/2019 |
1.4.0 | 111,382 | 5/24/2019 |
1.3.2 | 25,406 | 11/5/2018 |
1.3.1 | 11,299 | 2/24/2018 |
1.2.0 | 9,940 | 2/9/2017 |
1.1.0 | 2,146 | 8/4/2016 |
1.0.0 | 2,233 | 8/4/2016 |
0.9.0-rc | 2,131 | 8/4/2016 |