C# 12: Preview of Three New Features Coming

MMS Founder
MMS Almir Vuk

Article originally posted on InfoQ. Visit InfoQ

Microsoft has published a detailed release post that announces three new features that will be part of the upcoming release of C# 12. While still in the preview version, the C# 12 version introduces the features like primary constructors (for non-record classes and structs), using aliases for any type, and default values for lambda expression parameters.

The first highlight is the ability to use primary constructors which allows adding parameters to the class declaration and use them in the class body. This feature was previously only available for record type in C# 9 as part of the positional syntax for records, but it will now be extended to all classes and structs. With primary constructors, developers now can use the parameters to initialize properties or to include them in the code of methods and property accessors.

This feature is designed to simplify the process of creating and initializing objects in C#, allowing for more concise and readable code. The following code example shows how a primary constructor can be used in C# 12:

public class Student(int id, string name, IEnumerable grades)
{
    public Student(int id, string name) : this(id, name, Enumerable.Empty()) { }
    public int Id => id;
    public string Name { get; set; } = name.Trim();
    public decimal GPA => grades.Any() ? grades.Average() : 4.0m;
}

(Code sample provided by Microsoft devblogs.microsoft.com)

In addition to primary constructors, the default values for lambda expression parameters are another preview version feature. With this feature, developers can now specify and define default values for lambda expression parameters using the same syntax as for other default parameters. The default value will be emitted in metadata and is available via reflection as the DefaultValue of the ParameterInfo of the lambda’s Method property.

Before this preview release, if a developer wanted to provide default values for lambda expression parameters, they had to use local functions or a method called DefaultParameterValue from a specific namespace called System.Runtime.InteropServices.

Kathleen Dollard, Principal Program Manager, .NET, the author of the original blog post states the following:

These approaches still work but are harder to read and are inconsistent with default values on methods. With the new default values on lambdas you’ll have a consistent look for default parameter values on methods, constructors and lambda expressions.

Moving on to, the third highlight of C# 12 preview is a feature that enables the way to provide an alias to any type. Starting from this version, developers will be able to use using directives to abstract actual types and provide friendly names for “confusing or long generic names”. By having these aliases, developers can improve the readability of their code and make it easier to understand.

This feature allows developers to give alias names to almost any type, including nullable value types and tuples, like in the following code sample:

using Measurement = (string Units, int Distance);
using PathOfPoints = int[];
using DatabaseInt = int?;

public void F(Measurement x){ ... }

(Code sample provided by Microsoft devblogs.microsoft.com)

In addition to this, a user named Muhammad Miftah wrote an interesting comment regarding the usage of aliases. On April 11, 2023, the user wrote the following:

using type aliases seem to only work within the file, unless you define it as a global using, but then the global scope is polluted.

I think you should introduce a namespace-wide equivalent, maybe introduce a type keyword for that? Also it would be extra cool to also have the option to reify the type alias into something that exists and is reflection-queryable at runtime. Also usable in generics and generic type constraints. TypeScript already has this!

Furthermore, the comments on the post indicate a significant level of interest in the C# 12 version and upcoming releases. Some commenters have requested further details and clarifications on the features, while others have provided suggestions for additional ones. However, there are also users who expressed their lack of interest and critics who express concern about the rapid development of this programming language.

Thus, it is recommended for readers to take a look in the comments section since it is very insightful and it brought a lot of discussion between the users and author, including a lot of code samples and information regarding the future evolution of this programming language.

Also, to test these out users need to download the latest Visual Studio 17.6 preview or the latest .NET 8 preview. Microsoft is calling the community members to provide their feedback about mentioned features: primary constructors, using aliases for any type, and default values for lambda expression parameters.

Lastly, interested users can track the implementation progress of C# 12 through the Roslyn Feature Status page and follow the design process of this programming language through the CSharpLang GitHub repository page.

About the Author

Subscribe for MMS Newsletter

By signing up, you will receive updates about our latest information.

  • This field is for validation purposes and should be left unchanged.