C# 9 – What‘s the cool stuff?
Christian Nagel
https://csharp.christiannagel.com
Christian Nagel
• Training
• Coaching
• Consulting
• Development
• Microsoft MVP
• www.cninnovation.com
• csharp.christiannagel.com
• @christiannagel
Topics
• C# 9 Features
• Less coding
C# 8
• Readonly members
• Default interface methods
• Pattern matching enhancements
• Switch expressions
• Property patterns
• Tuple patterns
• Positional patterns
• Using declarations
• Static local functions
• Disposable ref structs
• Nullable reference types
• Asynchronous streams
• Asynchronous disposable
• Indices and ranges
• Null-coalescing assignment
• Unmanaged constructed types
• Stackalloc in nested expressions
• Enhancement of interpolated
verbatim strings
C# 9
• Records
• Init-only setters
• Top-level statements
• Covariant return types
• Target-typed new expressions
• Pattern matching enhancements
• Static anonymous functions
• Attributes on local functions
• Extension GetEnumerator
• Lambda discard parameters
• Native sized integers
• Function pointers
• Module initializers
• Extensions for partial methods
• Source code generators
A new Hello, World!
Top-level Statements
• Program class, Main method created from compiler
• Supports args
• Supports await
• Great for learning, interactive C#
• Great for small applications & services
• Extensions for Route to Code
Nullable Reference Types
• Available since C# 8
• Adoption phase from C# 8 to .NET 5
• .NET is annotated!
• Nullability still needs to be enabled (breaking changes)
Records
• Nominal records
• IEquality comparison & GetHashCode
• Copy constructor & Clone method
• with expressions
• Positional records
• Primary constructor – creates init-only set accessors
• Deconstruction
Target-typed new expression
• Book b = new Book();
• var b = new Book();
• Book b = new();
Source Generators
• Inject code, e.g. INotifyPropertyChanged
• Different sources for code generation (e.g. PInvoke)
• Reduce reflection
• Invoked by the C# Compiler
• Generated code can be used by the application
• Enhanced partial keyword
Summary
Top-level statements
Records
Nullable Reference Types
Increase productivity
Get better performance
For
action
https://csharp.christiannagel.com
https://github.com/ProfessionalCSharp
https://github.com/christiannagel/bastaspring2021
Thank You!

C# 9 - What's the cool stuff? - BASTA! Spring 2021

  • 1.
    C# 9 –What‘s the cool stuff? Christian Nagel https://csharp.christiannagel.com
  • 2.
    Christian Nagel • Training •Coaching • Consulting • Development • Microsoft MVP • www.cninnovation.com • csharp.christiannagel.com • @christiannagel
  • 3.
    Topics • C# 9Features • Less coding
  • 4.
    C# 8 • Readonlymembers • Default interface methods • Pattern matching enhancements • Switch expressions • Property patterns • Tuple patterns • Positional patterns • Using declarations • Static local functions • Disposable ref structs • Nullable reference types • Asynchronous streams • Asynchronous disposable • Indices and ranges • Null-coalescing assignment • Unmanaged constructed types • Stackalloc in nested expressions • Enhancement of interpolated verbatim strings
  • 5.
    C# 9 • Records •Init-only setters • Top-level statements • Covariant return types • Target-typed new expressions • Pattern matching enhancements • Static anonymous functions • Attributes on local functions • Extension GetEnumerator • Lambda discard parameters • Native sized integers • Function pointers • Module initializers • Extensions for partial methods • Source code generators
  • 6.
  • 7.
    Top-level Statements • Programclass, Main method created from compiler • Supports args • Supports await • Great for learning, interactive C# • Great for small applications & services • Extensions for Route to Code
  • 8.
    Nullable Reference Types •Available since C# 8 • Adoption phase from C# 8 to .NET 5 • .NET is annotated! • Nullability still needs to be enabled (breaking changes)
  • 9.
    Records • Nominal records •IEquality comparison & GetHashCode • Copy constructor & Clone method • with expressions • Positional records • Primary constructor – creates init-only set accessors • Deconstruction
  • 10.
    Target-typed new expression •Book b = new Book(); • var b = new Book(); • Book b = new();
  • 11.
    Source Generators • Injectcode, e.g. INotifyPropertyChanged • Different sources for code generation (e.g. PInvoke) • Reduce reflection • Invoked by the C# Compiler • Generated code can be used by the application • Enhanced partial keyword
  • 12.
    Summary Top-level statements Records Nullable ReferenceTypes Increase productivity Get better performance
  • 13.
  • 14.