Paul Krill
Editor at Large

C# 12 brings simpler syntax

news
Nov 15, 20232 mins
C#Microsoft .NETProgramming Languages

C# 12 simplifies code with collection expressions, primary constructors for all classes and structs, syntax to alias any type, and default parameters for lambda expressions.

A child practices handwriting the alphabet.
Credit: PeopleImages / Getty Images

C# 12, the latest edition of Microsoft’s popular object-oriented programming language, is now available as a production release, offering a simplified syntax and faster program execution.

C# 12 was unveiled as part of the .NET 8 software development platform on November 14. Developers can access it by downloading .NET 8, the Visual Studio 2022, or the Visual Studio Code C# Dev Kit extension.

To simplify code, C# 12 introduces collection expressions, primary constructors for all classes and structs, syntax to alias any type, and default parameters for lambda expressions. Collection expressions introduce a new terse syntax to create common collection values. Microsoft in noting these capabilities stressed it has set a goal for new features to not degrade performance.

To make code execution faster, C# 12 adds ref readonly parameters and inline arrays. Adding ref readonly parameters provides a final combination of passing parameters by reference or by value. An argument to a ref readonly parameter must be a variable. Inline arrays, which are a struct-based, fixed-length array type, provide a safe way to work with memory buffers. Interceptors, an experimental feature available in preview, allow redirection of method calls.

C# also features an experimental attribute, SystemDiagnostics.CodeAnalysis.ExperimentalAttribute, that makes it clear when Microsoft is not yet committed to a feature or implementation. When code uses types or members that are experimental, an error will occur unless the calling code is also marked as experimental. Each use of ExperimentalAttribute has a diagnostic ID for suppressing the error for individual features by an explicit compiler option or #pragma so developers can explore the experimental feature. Types, members, and assemblies can be marked with ExperimentalAttribute.

C# 12 ranks fifth in the Tiobe index of programming language popularity this month, ahead of languages such as JavaScript and PHP. Preceding C# 12 was C# 11, released in November 2022, featuring string literals and generic math.