Microsoft, with the latest preview of its planned .NET 8 software development platform, is highlighting three new C# 12 language features: primary constructors for non-record classes and structs, the use of aliases for any type, and default values for lambda expression parameters.
These capabilities are included in .NET 8 Preview 3, which was released April 11. General availability of both C# 12 and .NET 8 is expected in November. Developers also can access the C# 12 features in the latest Visual Studio 17.6 preview.
With primary constructors, developers can add parameters to the class declaration itself and use these values in the class body. For example, developers could use the parameters to initialize properties or use the parameters in the code of methods and property accessors. Primary constructors were introduced for records in C# 9 as part of the positional syntax for records. C# 12 extends these to all structs and classes.
C# 12 also extends alias support to any type. Microsoft explained that developers can use the
using alias directive to alias any type, not just named types. Semantic aliases can be created for tuple types, array types, pointer types, or other unsafe types.
And C# 12 enhances lambda expressions by allowing developers to define default values for parameters. The syntax and rules are the same as adding default values for arguments to any local function or method. Microsoft described default values for lambda expressions as taking the next step in empowering lambda expressions.
.NET 8 Preview 3 also includes changes to build paths, workloads, Microsoft.Extensions, and containers, as well as performance improvements in the JIT compiler for Arm64 and dynamic PGO (Profile Guided Optimisation). .NET 7, published in November 2022 featured performance enhancements, a new version of C#, container and cloud-native tooling, and sweeping improvements to .NET MAUI and other .NET libraries and frameworks.