Ulid

The Ulid NuGet package is a .NET library for generating and working with ULIDs (Universally Unique Lexicographically Sortable Identifiers). This package provides a fast and efficient implementation of ULID for .NET applications.


  • Uniqueness: ULIDs are designed to be globally unique, reducing the risk of collisions.

  • Lexicographical Sorting: ULIDs can be sorted lexicographically, which is useful for indexing and sharding in databases.

  • Compactness: ULIDs are shorter and more efficient than UUIDs, making them easier to handle and store.

  • Monotonic Sort Order: ULIDs maintain a consistent sort order, which is beneficial for time-based data.


  1. Install the Ulid Package: You can install the package via the NuGet Package Manager or the Package Manager Console:

    Install-Package Ulid
  2. Generating ULIDs: Once installed, you can use the Ulid package to generate and work with ULIDs. Here's a simple example:

    using System;
    using UlidNet;
    
    class Program
    {
        static void Main(string[] args)
        {
            // Generate a new ULID
            var ulid = Ulid.NewUlid();
            Console.WriteLine(ulid); // Output: 01ARZ3NDEKTSV4RRFFQ69G5FAV
    
            // Parse a ULID
            var parsedUlid = Ulid.Parse("01ARZ3NDEKTSV4RRFFQ69G5FAV");
            Console.WriteLine(parsedUlid.Time); // Output: The timestamp part of the ULID
        }
    }
  3. Generating ULID with a Specific Timestamp: You can also generate a ULID with a specific timestamp:

    using System;
    using UlidNet;
    
    class Program
    {
        static void Main(string[] args)
        {
            var timestamp = new DateTimeOffset(2024, 1, 1, 0, 0, 0, TimeSpan.Zero).ToUnixTimeMilliseconds();
            var ulid = Ulid.NewUlid(timestamp);
            Console.WriteLine(ulid); // Output: A ULID with the specified timestamp
        }
    }

Example Usage in a Web API

Here's an example of how you might use ULID in an ASP.NET Core Web API to generate unique IDs for new records:

In this example, the Ulid.NewUlid method generates a unique identifier for each new record, ensuring that each record has a unique and sortable ID.

By using the Ulid NuGet package, you can efficiently generate and manage globally unique, sortable identifiers in your .NET applications.

Last updated