Microsoft unveils C# 12 language enhancements

Three new features for C# 12 are available in the latest .NET 8 and Visual Studio 17.6 previews.

astract code [Thinkstock]
Thinkstock

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.

Microsoft is encouraging developers to provide feedback on the new C# features at the following links: primary constructors, alias any type, and default values in 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 Optimization). .NET 7, published in November 2022, also brought a new version of C#, along with performance enhancements, container and cloud-native tooling, and sweeping improvements to .NET MAUI and other .NET libraries and frameworks.

Copyright © 2023 IDG Communications, Inc.