DEV Community

Kiah Imani 🇧🇧
Kiah Imani 🇧🇧

Posted on • Edited on

Here's What’s New in .NET 9!

TL;DR: .NET 9 is like a phone upgrade. Better battery, improved performance, but no mind-blowing features. That’s okay, though! The past few versions have already handled a lot of heavy lifting.

Biggest bummer? Implicit extension types (the feature that could’ve really changed how we write code) got pulled from C# 13. Guess we’ll have to wait for the next drop. 😔

Now, let’s talk about the cool stuff!

LINQ Upgrades

  • LINQ Index: Want to loop through a collection and keep track of indexes? .NET 9 makes it simple:
var fruits = new List<string> { "Apple", "Banana", "Cherry" };

foreach (var (index, fruit) in fruits.Index()) {
    Console.WriteLine($"{index + 1}: {fruit}");
}
Enter fullscreen mode Exit fullscreen mode

✨ Output:

1: Apple  
2: Banana  
3: Cherry 
Enter fullscreen mode Exit fullscreen mode
  • LINQ CountBy: Counting items just got easier!
var pets = new List<string> { "Dog", "Cat", "Dog", "Bird" };

foreach (var count in pets.CountBy(p => p)) {
    Console.WriteLine($"There are {count.Value} {count.Key}s.");
}
Enter fullscreen mode Exit fullscreen mode

✨ Output:


There are 2 Dogs.  
There are 1 Cat.  
There are 1 Bird. 
Enter fullscreen mode Exit fullscreen mode

Let's just ignore the fact that in some of these cases, 'are' should be 'is'.

LINQ AggregateBy:

Need to calculate totals? AggregateBy has your back:

var orders = new List<(string Customer, int Amount)> {
    ("Alice", 50), ("Alice", 30), ("Bob", 20)
};

foreach (var total in orders.AggregateBy(o => o.Customer, 0, (sum, o) => sum + o.Amount)) {
    Console.WriteLine($"{total.Key} spent a total of ${total.Value}.");
}
Enter fullscreen mode Exit fullscreen mode

✨ Output:

Alice spent a total of $80.  
Bob spent a total of $20.  
Enter fullscreen mode Exit fullscreen mode

UUID Version 7

Need GUIDs that play nice with database indexes? Version 7 GUIDs are here for you:

var id = Guid.CreateVersion7();
Console.WriteLine(id);
Enter fullscreen mode Exit fullscreen mode

These sequential GUIDs make querying in databases faster. 🚀

Base Class Library Updates

HTTP/3 and QUIC
HTTP/3 is the hot new thing, and it’s enabled by default in .NET 9. 🌟
Here’s what you need to know:

HttpClient supports HTTP/3 if your system has MsQuic ready to roll.
Kestrel in ASP.NET Core? Fully on board.
IIS? Works with Windows Server 2022 if you’ve got TLS 1.3 configured and registry settings updated.
🤔 But heads up: If you’re using Visual Studio’s default HTTPS certs, browsers won’t play nice with HTTP/3. Keep that in mind when testing!

Lock

.NET 9 introduces a new Lock type that’s more efficient and future-ready:

private readonly Lock syncLock = new();

void UpdateInventory() {
    lock (syncLock) {
        // Thread-safe updates here
    }
}
Enter fullscreen mode Exit fullscreen mode

No more worrying about making your lock logic future-proof!

What’s New in C# 13?

Implicit Index Access

Initialize arrays with reverse indexing, effortlessly:

var secretCode = new byte[4] {
    [^1] = 0x42, 
    [^2] = 0x37,
    [^3] = 0x29, 
    [^4] = 0x18
};
Enter fullscreen mode Exit fullscreen mode

Boom—values set in reverse, no extra steps.

What’s New in ASP.NET Core 9?

Hybrid Cache

In-process and out-of-process caching? Yes, please.

builder.Services.AddHybridCache();

public async Task<string> GetDataAsync(string key) {
    return await hybridCache.GetOrCreateAsync(key, async entry => {
        await Task.Delay(100); // Fake fetch  
        return "Cached Result";
    });
}
Enter fullscreen mode Exit fullscreen mode

Avoid cache stampedes like a pro.

MapStaticAssets

Forget manually handling static files—.NET 9 optimizes this for you.

app.MapStaticAssets();
Enter fullscreen mode Exit fullscreen mode

Why it’s 🔥: Automatically calculated hashes (ETags) for files mean better caching. Your CI/CD pipeline says thank you.

Wrapping Up

.NET 9 may not have jaw-dropping new features, but it’s leveling up performance and usability in subtle, powerful ways. Think of it like a fine-tuned engine—ready to take your apps even further.

If you’re hyped (or just curious), let me know! Drop a comment, share your favorite features, or hit me with your best dev memes.

Let’s Keep Building Together!

Love geeking out about .NET, dev tips, or the latest in tech? Head to my site at blkgrlcto.com for updates and resources, and follow me on Twitter to stay in the loop. Let’s keep the innovation going and shape the future of development together! 🚀

Top comments (0)