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.
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.
usingdirectives simplify how to work with namespaces. In C# 10, a global
usingdirective 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
usingdirectives for the type of project being built. To enable implicit usings, developers must set the
ImplicitUsingsproperty 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 a
ToStringimplementation for derived records.
withexpresions 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.
Copyright © 2021 IDG Communications, Inc.