Paul Krill
Editor at Large

C# 12 brings simpler syntax

news
Nov 15, 20232 mins

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.

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorldโ€™s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorldโ€™s audience of software developers and other information technology professionals. Paul has won a โ€œBest Technology News Coverageโ€ award from IDG.

More from this author