Nullable reference types, async streams, ranges and the shape of modern pattern matching.
This release added 16 features, each with a before-and-after example.
Features in C# 8.0
- Async streams - Create and consume streams asynchronously.
- Default interface methods - Allow interfaces to evolve by providing default implementations of methods.
- Null coalescing assignment - A short syntax for assigning a value to an object if it is null.
- Null-forgiving operator - Suppress nullable reference type warnings when you know a value isn't null.
- Nullable reference types - Allow use of reference types to be declared nullable or non-nullable.
- Pattern-based using and disposable ref structs - Support using statements for types that offer a Dispose method without implementing IDisposable.
- Property pattern - Match against object properties within pattern matching expressions.
- Ranges and indices - Use
..and^operators to slice collections and access elements from the end. - readonly instance members - Allow struct members that do not modify state to be annotated as such.
- Recursive pattern matching - Match against properties and nested object structures in patterns.
- stackalloc in nested expressions - Allow a stackalloc expression anywhere an expression is permitted, not just as a direct initializer.
- Static local functions - Allow local functions to be declared static so they do not capture the class.
- Switch expressions - A switch designed to return values directly within a method.
- Switch tuple pattern - Match against multiple values simultaneously using tuple patterns.
- Unmanaged constructed types - Allow a generic type built from unmanaged type arguments to satisfy the unmanaged constraint.
- Using declarations - Simplify disposable resource management without nesting.