C# 9 was released in November 2020 as part of .NET 5. In this article, we will discuss some of the new features in C# 9.0.
Table of Contents
C# 9 Major Features
Records
A record is a value type that represents an immutable data structure. Records allow you to define a type that is a lightweight alternative to a class, with automatic properties and a concise syntax for defining them.
Init-Only Properties
Init-only properties allow you to set the value of a property only during object initialization, making it easier to create immutable objects.
Top Level Statements
C# 9 introduces the ability to define statements at the top level of a file, allowing you to write short scripts or utilities without the need for a class or a Main method.
Target-Typed New Expressions
C# 9 allows you to omit the type of an object when calling the new operator, allowing the type to be inferred from the context.
Improved Pattern Matching
C# 9 includes several improvements to the pattern matching feature, including support for property patterns, record patterns, and positional patterns.
Improved Interpolated Strings
C# 9 includes several improvements to interpolated strings, including the ability to embed expressions directly in a string, and support for string interpolation in attribute arguments.
Asynchronous streams
C# 9 introduces the IAsyncEnumerable and IAsyncEnumerator interfaces, which allow you to define and consume asynchronous streams of data.
Improved module support
C# 9 includes several improvements to the support for modules, including the ability to import and export top-level statements, and the ability to specify module dependencies.
Improved Nullable Reference Types
C# 9 includes several improvements to the nullable reference types feature, including support for non-nullable reference types, and the ability to specify nullability for indexers and events.
Leave a Reply