About
.NET 9 is the current latest release version of Microsoft. It is an open-source development platform for building applications for web, mobile, desktop, and cloud environments. This release is packed with many new features, LINQ methods performance improvements, and many more.
We can explore some of the features in the following links.
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.
![Microsoft]()
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.