ByteAether.Ulid
1.3.0
Prefix Reserved
See the version list below for details.
dotnet add package ByteAether.Ulid --version 1.3.0
NuGet\Install-Package ByteAether.Ulid -Version 1.3.0
<PackageReference Include="ByteAether.Ulid" Version="1.3.0" />
<PackageVersion Include="ByteAether.Ulid" Version="1.3.0" />
<PackageReference Include="ByteAether.Ulid" />
paket add ByteAether.Ulid --version 1.3.0
#r "nuget: ByteAether.Ulid, 1.3.0"
#:package ByteAether.Ulid@1.3.0
#addin nuget:?package=ByteAether.Ulid&version=1.3.0
#tool nuget:?package=ByteAether.Ulid&version=1.3.0
ULID
from ByteAether
A high-performance, fully compliant .NET implementation of ULIDs (Universally Unique Lexicographically Sortable Identifiers), adhering to the official ULID specification.
For more detailed documentation, visit our GitHub repository.
Features
- Universally Unique: Ensures global uniqueness across systems.
- Sortable: Lexicographically ordered for time-based sorting.
- Fast and Efficient: Optimized for high performance and low memory usage.
- Specification-Compliant: Fully adheres to the ULID specification.
- Interoperable: Includes conversion methods to and from GUIDs, Crockford's Base32 strings, and byte arrays.
- Ahead-of-Time (AoT) Compilation Compatible: Fully compatible with AoT compilation for improved startup performance and smaller binary sizes.
- Error-Free Generation: Prevents
OverflowException
by incrementing the timestamp component when the random part overflows, ensuring continuous unique ULID generation.
Installation
Install the latest stable package via NuGet:
dotnet add package ByteAether.Ulid
To install a specific preview version, use the --version
option:
dotnet add package ByteAether.Ulid --version <VERSION_NUMBER>
Usage
Here is a basic example of how to use the ULID implementation:
using System;
using ByteAether.Ulid;
// Create a new ULID
var ulid = Ulid.New();
// Convert to byte array and back
byte[] byteArray = ulid.ToByteArray();
var ulidFromByteArray = Ulid.New(byteArray);
// Convert to GUID and back
Guid guid = ulid.ToGuid();
var ulidFromGuid = Ulid.New(guid);
// Convert to string and back
string ulidString = ulid.ToString();
var ulidFromString = Ulid.Parse(ulidString);
Console.WriteLine($"ULID: {ulid}, GUID: {guid}, String: {ulidString}");
API
The Ulid
implementation provides the following properties and methods:
Creation
Ulid.New(GenerationOptions? options = null)
Generates a new ULID using default generation options. Accepts an optionalGenerationOptions
parameter to customize the generation behavior.Ulid.New(DateTimeOffset dateTimeOffset, GenerationOptions? options = null)
Generates a new ULID using the specifiedDateTimeOffset
and default generation options. Accepts an optionalGenerationOptions
parameter to customize the generation behavior.Ulid.New(long timestamp, GenerationOptions? options = null)
Generates a new ULID using the specified Unix timestamp in milliseconds (long
) and default generation options. Accepts an optionalGenerationOptions
parameter to customize the generation behavior.Ulid.New(DateTimeOffset dateTimeOffset, ReadOnlySpan<byte> random)
Generates a new ULID using the specifiedDateTimeOffset
and a pre-existing random byte array.Ulid.New(long timestamp, ReadOnlySpan<byte> random)
Generates a new ULID using the specified Unix timestamp in milliseconds (long
) and a pre-existing random byte array.Ulid.New(ReadOnlySpan<byte> bytes)
Creates a ULID from an existing byte array.Ulid.New(Guid guid)
Create from existingGuid
.
Checking Validity
Ulid.IsValid(string ulidString)
Validates if the given string is a valid ULID.Ulid.IsValid(ReadOnlySpan<char> ulidString)
Validates if the given span of characters is a valid ULID.Ulid.IsValid(ReadOnlySpan<byte> ulidBytes)
Validates if the given byte array represents a valid ULID.
Parsing
Ulid.Parse(ReadOnlySpan<char> chars, IFormatProvider? provider = null)
Parses a ULID from a character span in canonical format. TheIFormatProvider
is ignored.Ulid.TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out Ulid result)
Tries to parse a ULID from a character span in canonical format. Returnstrue
if successful.Ulid.Parse(string s, IFormatProvider? provider = null)
Parses a ULID from a string in canonical format. TheIFormatProvider
is ignored.Ulid.TryParse(string? s, IFormatProvider? provider, out Ulid result)
Tries to parse a ULID from a string in canonical format. Returnstrue
if successful.
Properties
Ulid.Empty
Represents an empty ULID, equivalent todefault(Ulid)
andUlid.New(new byte[16])
.Ulid.DefaultGenerationOptions
Default configuration for ULID generation when no options are provided by theUlid.New(...)
call..Time
Gets the timestamp component of the ULID as aDateTimeOffset
..TimeBytes
Gets the time component of the ULID as aReadOnlySpan<byte>
..Random
Gets the random component of the ULID as aReadOnlySpan<byte>
.
Conversion Methods
.AsByteSpan()
Provides aReadOnlySpan<byte>
representing the contents of the ULID..ToByteArray()
Converts the ULID to a byte array..ToGuid()
Converts the ULID to aGuid
..ToString(string? format = null, IFormatProvider? formatProvider = null)
Converts the ULID to a canonical string representation. Format arguments are ignored.
Comparison Operators
- Supports all comparison operators:
==
,!=
,<
,<=
,>
,>=
. - Implements standard comparison and equality methods:
CompareTo
,Equals
,GetHashCode
. - Provides implicit operators to and from
Guid
.
GenerationOptions
The GenerationOptions
class provides detailed configuration for ULID generation, with the following key properties:
Monotonicity
Controls the behavior of ULID generation when multiple identifiers are created within the same millisecond. It determines whether ULIDs are strictly increasing or allow for random ordering within that millisecond. Available options include:NonMonotonic
,MonotonicIncrement
(default),MonotonicRandom1Byte
,MonotonicRandom2Byte
,MonotonicRandom3Byte
,MonotonicRandom4Byte
.InitialRandomSource
AnIRandomProvider
for generating the random bytes of a ULID. The defaultCryptographicallySecureRandomProvider
ensures robust, unpredictable ULIDs using a cryptographically secure generator.IncrementRandomSource
AnIRandomProvider
that provides randomness for monotonic random increments. The defaultPseudoRandomProvider
is a faster, non-cryptographically secure source optimized for this specific purpose.
This library comes with two default IRandomProvider
implementations:
CryptographicallySecureRandomProvider
UtilizesSystem.Security.Cryptography.RandomNumberGenerator
for high-quality, cryptographically secure random data.PseudoRandomProvider
A faster, non-cryptographically secure option based onSystem.Random
, ideal for performance-critical scenarios where cryptographic security is not required for random increments.
Custom IRandomProvider
implementations can also be created.
Integration with Other Libraries
ASP.NET Core
Supports seamless integration as a route or query parameter with built-in TypeConverter
.
System.Text.Json
Includes a JsonConverter
for easy serialization and deserialization.
Other Libraries
Check out README in GitHub repository for examples to integrate with Entity Framework Core, Dapper, MessagePack, and Newtonsoft.Json.
Prior Art
Much of this implementation is either based on or inspired by existing works. This library is standing on the shoulders of giants.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. 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 is compatible. 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 is compatible. 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 is compatible. 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 is compatible. |
.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)
- System.Runtime.CompilerServices.Unsafe (>= 6.1.2)
-
.NETStandard 2.1
- System.Runtime.CompilerServices.Unsafe (>= 6.1.2)
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated | |
---|---|---|---|
1.3.1 | 96 | 8/13/2025 | |
1.3.0 | 107 | 7/31/2025 | |
1.2.0 | 346 | 6/25/2025 | |
1.1.1 | 146 | 6/24/2025 | |
1.1.0 | 783 | 4/16/2025 | |
1.0.0 | 2,838 | 1/21/2025 | |
0.2.2-alpha | 188 | 1/8/2025 | |
0.2.1-alpha | 196 | 12/30/2024 | |
0.2.0-alpha | 186 | 12/20/2024 | |
0.1.2-alpha | 196 | 11/29/2024 | |
0.1.1-alpha | 188 | 9/7/2024 | |
0.1.0-alpha | 189 | 8/30/2024 |