Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

What's New in C# 8

With the release of C# 8, the long-awaited safe handling of null values in reference types has finally become a part of our favorite programming language. But that's not the only important new feature in this version. There's expanded support for pattern matching, asynchronous streams, default interface methods, and more. In this session, I will explain what all these features are and show you how they can be used with some sample code.

  • Login to see the comments

What's New in C# 8

  1. 1. What’s new in C# 8? Damir Arh
  2. 2. Agenda New features Nullable reference types Improvements to pattern matching Asynchronous streams Default interface members Ranges and indexes Technical requirements
  3. 3. Nullable reference types Tony Hoare: “I call it my billion-dollar mistake. It was the invention of the null reference in 1965.” Null-safety through static analysis Syntactically similar to nullable value types Requires opt-in
  4. 4. Nullable reference types DEMO
  5. 5. Nullability in existing projects Two approaches Opt-in project, opt-out files Opt-in files one by one There’s more to it: notnull generic constraint Precondition attributes Postcondition attributes Flow attributes More details:
  6. 6. Pattern matching Originally introduced in C# 7 Enhanced in C# 8 Tuple patterns Property patterns Positional patterns Switch expression
  7. 7. Pattern matching DEMO
  8. 8. Asynchronous streams Iterators: IEnumerable, IEnumerator, yield return Asynchronous methods: async, await, Task Asynchronous streams: IAsyncEnumerable, IAsyncEnumerator, yield return async, await
  9. 9. Asynchronous streams DEMO
  10. 10. Asynchronous streams in use API controller action methods in ASP.NET Core 3.0 Asynchronous query results in EF Core 3.0 LINQ methods in Ix.NET
  11. 11. Default interface members Default method implementation in interface Why? Extend interface without breaking existing implementations Trait-like composition without multiple inheritance
  12. 12. Default interface members DEMO
  13. 13. Ranges and Indexes New types: Range, Index New expression syntax for both Implicit indexer syntax for existing types
  14. 14. Ranges and indexes DEMO
  15. 15. Ranges and indexes cheat sheet array 0 1 2 3 4 5 i 0 1 2 3 4 5 Length – i ^6 ^5 ^4 ^3 ^2 ^1 [ 1..5 ) Start: inclusive End: exclusive
  16. 16. Can I use C# 8 in my project? Supported in all popular IDEs Requires .NET Standard 2.1 .NET Core 3.0+ UWP, Xamarin, Unity, Mono No support in .NET framework
  17. 17. What about class libraries? Recommended target: .NET Standard 2.0 Reasons against it: Platform-specific application model C# 8 features Nullable annotations Not available in .NET Standard 2.0 Use multi-targeting
  18. 18. Resources What’s new in C# 8.0 Visual C# Archives | .NET Blog Language Feature Status The Absolutely Awesome Book on C# and .NET