C# 9.0 adds the following features and enhancements to the C# language:

  • Records
  • Init only setters
  • Top-level statements
  • Pattern matching enhancements
  • Native sized integers
  • Function pointers
  • Suppress emitting localsinit flag
  • Target-typed new expressions
  • static anonymous functions
  • Target-typed conditional expressions
  • Covariant return types
  • Extension GetEnumerator support for foreach loops
  • Lambda discard parameters
  • Attributes on local functions
  • Module initializers
  • New features for partial methods

C# 9.0 is supported on .NET 5. For more information, see C# language versioning.





Record types

C# 9.0 introduces record types, which are a reference type that provides synthesized methods to provide value semantics for equality. Records are immutable by default.

Record types make it easy to create immutable reference types in .NET. Historically, .NET types are largely classified as reference types (including classes and anonymous types) and value types (including structs and tuples). While immutable value types are recommended, mutable value types don’t often introduce errors. Value type variables hold the values so changes are made to a copy of the original data when value types are passed to methods.

There are many advantages to immutable reference types as well. These advantages are more pronounced in concurrent programs with shared data. Unfortunately, C# forced you to write quite a bit of extra code to create immutable reference types. Records provide a type declaration for an immutable reference type that uses value semantics for equality. The synthesized methods for equality and hash codes consider two records equal if their properties are all equal. Consider this definition:

Showing all 10 results

Showing all 10 results