Understanding UUID v7 in .NET 9

The most recent version of Microsoft software is.NET 9. It is an open-source development platform for creating desktop, cloud, mobile, and web apps. Numerous new features, performance enhancements for LINQ methods, and many more are included in this edition.

In this article, we are focusing on one of the features named UUID v7.

Introduction

In the earlier versions of .NET, we’ve used Guid.NewGuid() (version 4) method generates UUIDs. It simply generates a unique identifier, which is used in our projects. The present .NET 9 version supports UUID v7 format of code like Guid.CreateVersion7(). This generated UUID incorporated a timestamp already in it. So, It is really very helpful when we store this UUID at the database level to sort it based on creation time.

The structure of the UUID format is as follows.

48-bit timestamp || 12-bit random || 62-bit random

  • 48-bit timestamp: It represents the number of milliseconds since the Unix epoch. So, It provides info about creation time.
  • 12-bit random: It adds randomness to ensure uniqueness within the same millisecond.
  • 62-bit random: It also ensures overall uniqueness by offering randomness of entropy.

Code Examples

Below is the usual example of creating UUID in C# before the .NET 9 approach.

var guid = Guid.NewGuid(); // v4 UUID

The present .NET9 version to create UUID v7 format is as follows.

var guidv7 = Guid.CreateVersion7();  // v7 UUID

This version default inherits the UtcNow time format. Let’s also understand the Sort feature by creating multiple UUIDs in a custom way. So, for this scenario, we are going to create a few UUIDs with different times and then apply the sort feature. The below code block provides more details on this.

public static void ExploreUUID()
{
    var guid = Guid.NewGuid(); // v4 UUID
    var guidv7 = Guid.CreateVersion7(); // v7 UUID
    var guidList = new List<Guid>
    {
        Guid.CreateVersion7(TimeProvider.System.GetUtcNow()),
        Guid.CreateVersion7(TimeProvider.System.GetUtcNow().AddMinutes(-10)),
        Guid.CreateVersion7(TimeProvider.System.GetUtcNow().AddMinutes(10)),
        Guid.CreateVersion7(TimeProvider.System.GetUtcNow().AddMinutes(-20))
    };
    // Write the GUIDs in whatever list contains.
    foreach (var v7guid in guidList)
    {
        Console.WriteLine(v7guid.ToString());
    }
    Console.WriteLine("=====================");
    // Order the GUID list and then write the list.
    // The result you can identify GUIDs are ordered based on creation time.
    var sortedList = guidList.OrderBy(x => x).ToList();
    foreach (var v7guid in sortedList)
    {
        Console.WriteLine(v7guid.ToString());
    }
}

The code output is as follows.

The aforesaid source code UUIDSample.zip file is also attached to this article.

Summary

We have explored the UUID v7 feature in .NET 9 and its advantages of inbuilt timestamp, which can be really helpful to order created UUIDs order in the database level.

This creation/usage of UUID v7 (CreateVersion7()) takes a bit more time than the earlier version (NewGuid()). However, This time is very negligible unless we are creating millions of UUIDs. So, please keep this in consideration, and let us start using it in our project and benefit.

Best and Most Recommended ASP.NET Core 8.0.11 Hosting

Fortunately, there are a number of dependable and recommended web hosts available that can help you gain control of your website’s performance and improve your ASP.NET Core 8.0.11 web ranking. HostForLIFE.eu is highly recommended. In Europe, HostForLIFE.eu is the most popular option for first-time web hosts searching for an affordable plan.

Their standard price begins at only €3.49 per month. Customers are permitted to choose quarterly and annual plans based on their preferences. HostForLIFE.eu guarantees “No Hidden Fees” and an industry-leading ’30 Days Cash Back’ policy. Customers who terminate their service within the first thirty days are eligible for a full refund.

By providing reseller hosting accounts, HostForLIFE.eu also gives its consumers the chance to generate income. You can purchase their reseller hosting account, host an unlimited number of websites on it, and even sell some of your hosting space to others. This is one of the most effective methods for making money online. They will take care of all your customers’ hosting needs, so you do not need to fret about hosting-related matters.