23 January 2023

23 January 2023

Tags:

.NET 7 Networking Improvements

Some low-level network improvements that make things "just work" as expected.

Code Optimization Using Span

Since strings are immutable, it's an unnecessary allocation of a new string to call substring of a string. By using Slice instead, you get a window into a subset of an existing memory range.

Instead of:

string dateString = "12-03-2022";
var d = dateString.Substring(3, 2);

do this:

ReadOnlySpan<char> dateSpan = dateString;
var d = dateSpan.Slice(3, 2);

ArgumentException helpers

A new method was added in .Net 6: ArgumentNullException.ThrowIfNull(arg). I didn't know that, but now I do and I'll try and use it.

In .Net 7 they've also added ArgumentException.ThrowIfNullOrEmpty.

The article goes on to propose some more methods, but the proposals have been rejected by the .Net team.

A first look behind the scenes of minimal API endpoints

Minimal APIs were introduced in .Net 6 and allow an app to be reduced to a few lines:

WebApplicationBuilder builder = new WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();

app.MapGet("/", () => "Hello world!");

app.Run();

This blob post looks at the code behind that makes this possible.

5 new MVC features in .NET 7

As well as minimal APIs, .Net 7 also added comparable support to MVC controllers, ie ways of parsing parameters from the request URI and body.