What's new in C# 10
I’m compiling information for our next Adelaide .NET User Group meetup which is focusing on the launch of .NET 6 and Visual Studio 2022.
Next up is C# 10.0. There’s a good summary on Microsoft Docs on the What’s new in C# 10.0 page. At the time of writing that page was still being updated, with the progress of documenting all features being tracked here.
Also refer to https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md for features implemented in Roslyn.
- Record structs
- Improvements of structure types
- Interpolated string handlers
global usingdirectives- File-scoped namespace declaration
- Extended property patterns
- Allow
constinterpolated strings - Record types can seal
ToString() - Allow both assignment and declaration in the same deconstruction
- Allow
AsyncMethodBuilderattribute on methods
Other features still to be documented include:
- Lambda improvements - Allow lambdas with attributes, allow lambdas with explicit return type, infer a natural delegate type for lambdas and method groups
- Caller argument expression - This would aid in debugging and diagnostic scenarios. It would allow methods to capture the expression passed to a method for diagnostic purposes.
- Extend the preprocessor
linedirective - allow specifying full span mapping - Improved definite assignment
- Update preprocessor symbols for .NET 6
- Documentation - Source generators
- Documentation - Best practice of when to use module initializer vs. static constructor
- Static abstract members in interfaces
These are also on the list to be documented, but I believe will be only available under a preview flag:
- Generic attributes - Generic classes can now specify
System.Attributeas a base class - Method parameter names scoping change for
nameof- enablenameofto name a parameter in an attribute constructor - Allow deconstruction of ‘default’ literal
- Simplified parameter null validation code
- List patterns
- Relax ordering constraints around ref and partial modifiers on type declarations
- Allow value tuple deconstruction with default keyword