Microsoft's C# 11 language upgrade nears the finish line

Object initialization and generic math get attention in the latest improvements to the well-known programming language.

Microsoft's C# 11 language upgrade nears the finish line
Thinkstock

C# 11, the planned next version of Microsoft’s popular object-oriented, type-safe programming language, is nearly complete and continues to add capabilities, including object-initialization improvements and generic math support.

With object initialization, developers can more easily support constructors and object initializers in their type, independent of rules to enforce for immutable and mutable members. Features include required members and ref fields. With generic math, meanwhile, developers can write algorithms once for multiple numeric types. This makes it easier to use C# and .NET for machine learning, statistics, and other math-intensive applications. Capabilities are included such as relaxed right-shift requirements, an unsigned right-shift operator, static abstract and static virtual members in interfaces, and numeric IntPtr].

Microsoft is touting these as new features added to the language in the Visual Studio 2022 17.3 IDE, which became available on August 9. Previous improvements in C# 11 covered areas such as raw string literals and list patterns. For developer productivity in C# 11, the nameof operator now can be used with method parameters, as of Visual Studio 2022 17.3. This enables developers to use this operator in attribute declarations on methods.

Also new in C# 11, the compiler ensures that fields of a struct type are initialized to their default values as part of executing a constructor. This means any field or auto property not initialized by a constructor is initialized automatically by the compiler. Structs where the constructor does not definitely assign all fields now compile, and any fields not explicitly initialized are set to their default value.

For pattern matching, the same pattern matching logic that has been used to test if a string had a specific constant value now can be used with variables that are Span<char> or ReadOnlySpan<char>.

To try the latest C# 11 features, developers can download the latest preview of Visual Studio 2022 and install the .NET 7 preview. Developers also can separately install the latest preview of .NET 7. A final release of .NET 7 is expected in November.

Copyright © 2022 IDG Communications, Inc.