Welcome .NET 10!
As a developer, I am constantly using ASP.NET Core in the backend, mainly for web APIs and cron jobs that handle data. Every year in November, Microsoft releases a new major version with added features and improvements. Here are some highlights from my perspective. Of course there are lots more, depending on your field of work.
General performance improvements
There have been a ton of performance improvements in the new version. So just upgrading a project will make it run faster and with less memory. Stephen Toub shows some examples with a simple benchmark comparison.
C# 14
With .NET 10 comes a new version of C#, the main programming language of the .NET world. Most of them make it easier to write code by introducing new shorter ways to express a known concept.
Null-conditional assignment (?.=)
Since objects can be nullable, any property access or assignment would fail if that is the case. C# 14 introduces a shorter way to make an assignment if the object exists.
Order? order = GetOrderIfExists();
// old syntax
if (order is not null)
{
order.IsReady = true;
}
// new syntax
order?.IsReady = true;
The field keyword for properties
Since the very first version of C# you can define properties with a backing field.
// old syntax
public class Person
{
// backing field for property FirstName
private string? _firstName;
// property definition
public string? FirstName
{
get => _firstName;
set { _firstName = value; OnFirstNameChanged(value); }
}
}
The new keyword field gets rid of the backing field, the example also uses an auto-property for get.
// new syntax
public class Person
{
// property definition
public string? FirstName
{
get;
set { field = value; OnFirstNameChanged(value); }
}
}
Extension blocks
Extension methods have been around for quite long, they allow to add a method to an existing class. The syntax uses static classes and methods with the keyword this.
// old syntax
static class PersonExtensions
{
// property definition
public static bool CanDrinkAlcohol(this Person person)
{
return person.Age >= 18;
}
}
// usage
var person1 = new Person { Age = 17 };
person1.CanDrinkAlcohol(); // should be false
This can be simplified with the new extension keyword. It omits the static keyword in the method declaration and reads almost like a class definition.
// new syntax
static class PersonExtensions
{
extension(Person person)
{
CanDrinkAlcohol => person.Age >= 18;
}
}
// usage
var person1 = new Person { Age = 17 };
person1.CanDrinkAlcohol(); // should be false
More new features can be found at the official page What’s new in C# 14.
Blazor
Blazor has also seen some improvements, namely the hot reload works now faster. Daniel Roth highlights more things in the video. To me, this does not look like a big change, but there are also some hidden performance optimizations which should make my life easier.
I am using Blazor in an internal tool for showing our logs and job runs/fails. It’s a good playground. I enjoy having direct access to the database with Entity Framework, eliminating the need for an extra layer (Web API) in between.