C# 10, the latest release of Microsoft’s object-oriented, type-safe programming language for the .NET platform, has arrived, with capabilities intended to make code “prettier,” quicker, and more expressive, the company said.
The upgrade to C# is part of the .NET 6 software development framework and Visual Studio 2022 IDE, both of which were published as production releases on November 8.
New features and improvements in C# 10 include the following:
- The C# 10 compiler understands code better and produces fewer spurious errors. Developers will see fewer spurious errors and warnings for null references.
- C#
using
directives simplify how to work with namespaces. In C# 10, a globalusing
directive and implicit usings reduce the number of usings needed to be specified at the top of each file. - Implicit usings, enabled in .NET 6 templates, add common
global
using
directives for the type of project being built. To enable implicit usings, developers must set theImplicitUsings
property in the .csproj file. - Namespaces can be included as a statement, followed by a semi-colon and without curly brackets.
- Improvements have been made to types and syntax surrounding lambdas. Lambda expressions now have a “natural” type, meaning the compiler often can infer the type of the lambda expression.
- Attributes can be put on lambda expressions in the same manner as methods and local functions.
- Improvements to structs provide better parity between structs and classes. These features include parameterless constructors, field initializers, record structs, and withcode expressions.
- Record classes have been improved. The
ToString()
method now includes the sealed modifier, preventing the compiler from synthesizing aToString
implementation for derived records. with
expresions are supported for all structs including record structs and anonymous types.- The syntax for interpolated strings has been improved for performance and expressiveness.
- Property patterns have been extended to make it easier to access nested values in patterns.
- Static members in interfaces can now be declared as abstract, providing the basis for a new set of generic math constraints that enable developers to abstract over which operators are available.