Friday, May 3, 2024

C# version 9 | What is new in C# version 9

Here is the link. 

C# version 9

Released November 2020

C# 9 was released with .NET 5. It's the default language version for any assembly that targets the .NET 5 release. It contains the following new and enhanced features:

C# 9 continues three of the themes from previous releases: removing ceremony, separating data from algorithms, and providing more patterns in more places.

Top level statements means your main program is simpler to read. There's less need for ceremony: a namespace, a Program class, and static void Main() are all unnecessary.

The introduction of records provides a concise syntax for reference types that follow value semantics for equality. You use these types to define data containers that typically define minimal behavior. Init-only setters provide the capability for nondestructive mutation (with expressions) in records. C# 9 also adds covariant return types so that derived records can override virtual methods and return a type derived from the base method's return type.

The pattern matching capabilities expanded in several ways. Numeric types now support range patterns. Patterns can be combined using andor, and not patterns. Parentheses can be added to clarify more complex patterns:

C# 9 includes new pattern matching improvements:

  • Type patterns match an object matches a particular type
  • Parenthesized patterns enforce or emphasize the precedence of pattern combinations
  • Conjunctive and patterns require both patterns to match
  • Disjunctive or patterns require either pattern to match
  • Negated not patterns require that a pattern doesn't match
  • Relational patterns require the input be less than, greater than, less than or equal, or greater than or equal to a given constant


No comments:

Post a Comment