Records, init-only setters, top-level statements and a large expansion of pattern matching.
This release added 16 features, each with a before-and-after example.
Features in C# 9.0
- Attributes on local functions - Apply attributes to local functions and their parameters.
- Covariant returns - Allow a method to return a more specific type than the method it overrides.
- Extending partial methods - Partial methods can have return values and out parameters.
- Extension GetEnumerator - Extension methods allow foreach to enumerate over new types.
- Function pointers - Call methods through pointers without delegate allocations in performance-critical code.
- Init-only setters - Specify properties that can only be set during construction.
- Lambda discard parameters - Signify no interest in a parameter to a lambda.
- Module initializers - Run code in an assembly when it is first loaded.
- Native integers - Provide architecture-specific integer types.
- Pattern matching improvements - Enhanced pattern matching with relational, logical, and simplified type patterns.
- Records - Declare immutable types and eliminate boilerplate code.
- Skip locals init - Prevent the CLR from zeroing local variables for performance.
- Static anonymous functions - Prevent an anonymous function from capturing state.
- Target-typed conditional expression - Eliminate casts in conditional operations by considering the result type.
- Target-typed new - Use new without a type if the compiler can infer it.
- Top-level statements - Skip Program.Main() for a script-like experience.