UUID v7 in .NET 9

.NET natively knows UUID v4 aka Guid.NewGuid(). But with preview 7 we get Version 7 of UUID's natively.

UUID v7

As GUID is just another valid alternative name for UUID, the new API is still inside System.Guid:

var guid = Guid.CreateVersion7();
var guidWithTimestamp = Guid.CreateVersion7(DateTimeOffset.UtcNow);

The main advantage is the timestamp that is included in the UUID. Why? Let's have a look how UUID v7 is structured:

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

That gives 122 bits entropy with 6 bits for version and variant somewhere in the middle. The main advantage is that you can sort UUIDs by their creation time making them a better fit for databases than UUID v4.

Controlling the Timestamp

As the method expects a DateTimeOffset a calling function can utilize a TimeProvider to control UtcNow:

var uuid = Guid.CreateVersion7(timeProvider.GetUtcNow());

timeProvider can be retrieved via the DI container and be faked in testing scenarios.

Resources

Keyed Services in the IServiceProvider in .NET 8 preview 7

The .NET 8 preview 7 will bring another exciting feature some of you probably awaiting for a long time: Keyed services.

MemoryCache, DistributedCache and HybridCache

The latest preview (.NET 9 preview 4) brought another caching structure to the .NET world - so let's order some things here.

ReadOnlySet<T> in .NET 9

The next preview (preview 6) will bring a new type ReadOnlySet<T>. This is a read-only set that is similar to ReadOnlyCollection<T>. Let's see how it works and why it was introduced.

An error has occurred. This application may no longer respond until reloaded. Reload x