Although .NET 7 brings another number of new features, the main emphasis this time was on performance. ASP.NET contains rate limiter, so we can usually do without the web.config
file. The 11th version of C# brings better work with strings. And one feature that was previously only available in UWP and Mono – compilation in AOT mode (ahead-of-time) – is now available for the entire framework.
Rate limiting
ASP.NET Core now has a way to limit the number of incoming HTTP requests.
builder.Services.AddRateLimiter(options => {
options.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
options.AddFixedWindowLimiter(...);
options.AddSlidingWindowLimiter(...);
options.AddTokenBucketLimiter(...);
options.AddConcurrencyLimiter(...);
});
app.UseRateLimiter();
See the documentation for details.
What’s new in C# 11?
Modifier required
If you have activated explicit non-nullable reference types, you must have already come across the following message in your classes: Non-nullable property 'Brand' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
class Car {
public string B̲r̲a̲n̲d̲ { get; set; }
public Color Color { get; set; }
}
But what if we don't want to create a constructor and a private variable just to initiate a class with a readonly value? We will use the modifier required!
class Car {
public required string Brand { get; init; }
public Color Color { get; set; }
}
And beware: Entity Framework maps the modifier required to the database as the data type NOT NULL.
Range operator
Working with strings is a little easier. Until now, a certain part of the chain was obtained as follows:
var str = "I'm missing Windows Phone!";
var iam = str.Substring(0, 3);
var missing = str.Substring(4, 7);
var wp = str.Substring(str.Length - 14);
Now you can work with the string in the same way as with an array in F# (in which it is called slices):
var str = "I'm missing Windows Phone!";
var iam = str[..3];
var missing = str[4..11];
var wp = str[^14..];
Beware that indexes are written as in the JavaScript function substring
. Thus, it is not the index and length that are written, but the start and end indexes. If we want to specify the index from the end (not from the beginning) of the string, we use ^
.
Also, be sure to read the traditional summary of performance improvements.