Favorite improvements in .NET 6

There is so much to look forward to in .NET 6 and with the open planning process you can see the upcoming releases with a hierarchical model of themes, epics, and user stories, with priorities and categories.

From my personal perspective .NET 6 represents the LTS release so there is a lot of support runway for me, and thus I will go ahead and spend some time to upgrading my open source projects. There is so much happening here in multiple areas but I think the Thread Pool changes and a few bits of Syntax Sugar that will impact me the most.

Thread Pool

Once a dev uses async, all of your dependent calls should be async, unfortunately the tendency for us to do async code badly leads to a scenario called “sync over async” in production. In fact being partially async can be worse than being entirely synchronous in some cases. .NET 6 has a more pragmatic view and assumes that mixing of sync and async may well be unavoidable. Given this the thread pool management becomes much more forceful about increasing its thread count to meet the needs of poorly constructed code (blocking detected). When synchronous blocking code is no longer detected the Thread pool can then immediately lower the target thread pool count.

Stephen Toub goes into way more details here.

Syntax Sugar

The obvious changes occur in the language, these updates may not inspire you to rewrite code but should certainly help you get this done faster and more correctly.  The first one I like is the repeated code we insert at the top of methods that checks for null, something like this:

void SomeImportantMethod(object obj)
    if(obj == null)
        throw new ArgumentNullException("obj");

That’s four lines, if you count my unnecessarily verbose brackets! Well now can shorten this to a single line, as follows:

void SomeImportantMethod(object obj)

I have also shared a lot of examples of async being done badly so it is with great relief that options are now present to help developers fall into the pit of successful asynchrony. In the following example we yield the await if the Task does not complete in 8 seconds. You can, in effect, enforce a hard limit for and Task:

Task grabTask = GatherLotsofDataAsync();
await grabTask.WaitAsync(TimeSpan.FromSeconds(8));

I love LINQ and the latest release provides some convenient spackling to help deal with selecting the Minimum and Maximum entries from a List:

public record DumbBell(int Weight);

List<dumbbell> dumbbells = GetListOfMyDumbBells();

var lightweight = dumbbells.MinBy(p => p.Weight);
var heavyweight = dumbbells.MaxBy(p => p.Weight);

Console.WriteLine($"The most I can lift is {heavyweight.Weight}");

So much more…

These are just the features impacting me the most, there is so much more here, please check out the following posts and subscribe to the .NET blog:

purple flower

Comment Section

Comments are closed.