Chapter 15. C# 8 and beyond

 

This chapter covers

  • Expressing null and non-null expectations for reference types
  • Using switch expressions with pattern matching
  • Matching patterns recursively against properties
  • Using index and range syntax for concise and consistent code
  • Using asynchronous versions of the using, foreach, and yield statements

At the time of this writing, C# 8 is still being designed. The GitHub repository shows a lot of potential features, but only a few have reached the stage of publicly available preview builds of the compiler. This chapter is educated guesswork; nothing here is set in stone. It’s almost inconceivable that all the features being considered would be included in C# 8, and I’ve restricted myself to the ones I consider reasonably likely to make the cut. I’ve provided the most detail about the features available in preview at the time of writing, but even so, that doesn’t mean further changes won’t occur.

Note

At the time of this writing, only a few C# 8 features are available in preview builds, and there are different builds with different features. The preview for nullable reference types supports only full .NET projects (rather than .NET Core SDK style projects), which makes it harder to experiment with them on real code if all your projects use the new project format. I expect these limitations to be overcome in later builds, possibly by the time you read this.

We’ll start with nullable reference types.

15.1. Nullable reference types

15.2. Switch expressions

15.3. Recursive pattern matching

15.4. Indexes and ranges

15.5. More async integration

15.6. Features not yet in preview

15.7. Getting involved

Conclusion