SlideShare a Scribd company logo
MANOJ KUMAR SHARMA
Platform & Developer Evangelist
mailme@manojkumarsharma.com
http://www.linkedin.com/in/contact4manoj
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission.
1
What’s new in C# 8.0
2
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Introduction
What’s new in C# 8.0
3
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
How to check available C# versions?
• Open Developer Command Prompt
 csc -langversion:?
Developer Command Prompt for VS2019Developer Command Prompt for VS2019
4
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Default Language Version
The Compiler determines
the DEFAULT Language
Version.
Target framework Version Default C# language version
.NET Core 3.x C# 8.0
.NET Core 2.x C# 7.3
.NET Standard 2.1 C# 8.0
.NET Standard 2.0 C# 7.3
.NET Standard 1.x C# 7.3
.NET Framework all C# 7.3
5
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
How to use C# 8.0
• To override the DEFAULT version to use C# 8.0:
• Edit the .csproj file
...
<PropertyGroup>
...
<OutputType>Exe</OutputType>
...
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<LangVersion>8.0</LangVersion>
...
</PropertyGroup>
...
.csproj.csproj
6
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Support modern cloud scenarios
• Async enumerables
• More patterns in more places
• Default interface members
• Indices and Ranges
7
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Increase your productivity
• Using statement
• Static local functions
• Readonly members
• Null coalescing assignment
• Unmanaged constraint
• Interpolated verbatim strings
8
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
What’s new in C# 8.0
• 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
• Indices and ranges
• Null-coalescing assignment
• Unmanaged constructed types
• stackalloc in nested expressions
• Enhancement of interpolated
verbatim strings
9
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Some of them….
What’s new in C# 8.0
10
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Readonly members
• Add the readonly modifier to any structure member.
• Indicates that the member does not modify state.
• It's more granular than applying the readonly modifier to a struct declaration.
• This feature lets you specify your design intent so the compiler can
enforce it, and make optimizations based on that intent.
cs8_con_ReadonlyMembers
11
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Default Interface Methods
• Used to define default method
implementations to members
implementing the Interface.
• Change Interfaces without breaking
changes
• Reusability of methods in
independent classes
• Based on Java’s Default Methods
• Extensions are now possible!
• Alternative to Extension Methods
• Runtime polymorphism
• Allowed Modifiers:
private, protected, internal,
public, virtual, abstract, override,
sealed, static, external
cs8_con_DefaultInterfaceMethods
12
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Ranges and Indices
• Based upon two new Types:
• System.Index
• Represents an index for a sequence of objects in collection
• The index from end operator ( ^ ) (known as “hat” operator)
• Works on Countable Types having Length / Count and an Instance Indexer with int
• System.Range
• Represents a sub-range of a sequence of objects in the collection
• The range operator ( .. ) specifies the start and end of a range as its operands
• Works on Countable Types having Length / Count and Slice() method with two int
• NOTE:
• Ranges is not supported by List<T>
• The element selection is:
• 0-based if you are counting from the beginning, and
• 1-based if you are counting from the end.
cs8_con_RangesAndIndices
13
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Null Reference Types
• Sir Charles Antony Richard Hoare
• Inventor of QuickSort Algorithm in 1959/1960
• In 2009 at QCon, London,
apologized for inventing Null Reference
• The most common .NET Exception – NullReferenceException
I call it my billion-dollar mistake. It was the invention of the null reference in
1965. At that time, I was designing the first comprehensive type system for
references in an object oriented language (ALGOL W). My goal was to ensure
that all use of references should be absolutely safe, with checking performed
automatically by the compiler. But I couldn't resist the temptation to put in a
null reference, simply because it was so easy to implement….
14
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Null Reference Types
• In C# 5.0: Coalescing Operator ( ?? ) was introduced to provide
Default Values for null
decimal basicSalary;
private void AddBonus(decimal? percent)
{
// Would throw NullReferenceException if percent is null!
basicSalary += (basicSalary * percent.Value);
// Solution: Check for null, before consumption
basicSalary += percent.HasValue ? (basicSalary * percent.Value) : 0M;
// Solution: C# 5.0 approach
basicSalary += basicSalary * (percent ?? 0M);
}
Employee.csEmployee.cs
15
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Null Reference Types
• In C# 6.0:
Null-Check Operator / Null-Safe Operator ( ?. ) simplified code
• NOTE:
( ?. ) returns a nullable value
See https://enterprisecraftsmanship.com/posts/3-misuses-of-operator-in-c-6/
for more information to understand when and how to use efficiently.
int? productsCount = products?.Length;
.cs.cs
16
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
How to use Nullable Reference Types
• To enable Nullable Reference Types in C# 8.0 Project:
...
<PropertyGroup>
...
<OutputType>Exe</OutputType>
...
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
...
</PropertyGroup>
...
.csproj.csproj
17
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
How to use Nullable Reference Types
• To enable at the file level:
• To disable at the file level:
#nullable enable
using System;
...
.cs.cs
#nullable disable
using System;
...
.cs.cs
18
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Nullable Reference Types
• To help mitigate NullReferenceException with Compiler Warnings:
( ?? ) Null-Coalescing Operator C# 5.0
( ! ) Postfix Unary Null-Forgiving Operator C# 8.0
( ??= ) Null-Coalescing Assignment Operator C# 8.0
( ?. ) Null-Coalescing Conditional Operator a.k.a. Null-Safe Operator C# 6.0
• Helps to find bugs; Flow analysis tracks nullable reference variables
See also:
• https://docs.microsoft.com/en-us/dotnet/csharp/nullable-attributes
• https://www.meziantou.net/csharp-8-nullable-reference-types.htm
• https://docs.microsoft.com/en-us/dotnet/csharp/nullable-attributes
• https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/nullable-reference-types
cs8_con_NullableReferenceTypes
19
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Nullable Reference Types
• Recommended Guidelines for adoption:
• Library developers – Nullable adoption phase before .NET 5
• App developers – nullability on your own pace
• Annotate new APIs
• Do not remove argument validation
• Parameter is non-nullable if parameters are checked
(ArgumentNullException)
• Parameter is nullable if documented to accept null
• Prefer nullable over non-nullable with disagreements
20
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Async Streams
• so far: async / await returns a result
• Async streams extends async / await stream of results
• Asynchronous data sources from the consumer to be controlled
• Alternative to Reactive Extensions (Rx) for .NET
System.Reactive (https://github.com/dotnet/reactive)
A library for composing asynchronous and event-based programs using
observable sequences and LINQ-style query operators
USE CASE:
• Streaming from Server to Client
• Streaming from Client to Server
21
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Async Streams
public interface IAsyncEnumerator<[NullableAttribute(2)] out T>
: IAsyncDisposable
{
T Current { get; }
ValueTask<bool> MoveNextAsync();
}
System.Collections.Generic.IAsyncEnumerator.csSystem.Collections.Generic.IAsyncEnumerator.cs
public interface IAsyncEnumerable<out T>
{
IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default);
}
System.Collections.Generic.IAsyncEnumerable.csSystem.Collections.Generic.IAsyncEnumerable.cs
public interface IAsyncDisposable
{
ValueTask DisposeAsync();
}
System.IAsyncDisposable.csSystem.IAsyncDisposable.cs
cs8_con_AsyncStreams
22
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Summary
What’s new in C# 8.0
23
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
C# 8.0
Safe
Nullable and non-nullable
reference types help you write
safer code. Declare your intent
more clearly.
Modern
Async streams for modern
workloads like Cloud & IoT
communication.
Easily work with cloud scale
datasets using Indexes and
Ranges.
Productive
Write less code using Patterns.
Protect data with readonly
members.
Improved using statements for
resource management.
24
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be
reproduced in any form without an express written permission.
Further Learning…
• What’s New in C# 8.0
https://aka.ms/new-csharp
• .NET CONF 2019 official website
https://www.dotnetconf.net
• Videos on Youtube:
http://bit.ly/bdotnetconf2019
• All .NET CONF 2019 Materials:
https://github.com/dotnet-presentations/dotnetconf2019
• To edit “Edit Project File” and other useful extensions in VS2019
Power Commands for Visual Studio – Microsoft DevLabs
https://marketplace.visualstudio.com/items?itemName=VisualSt
udioPlatformTeam.PowerCommandsforVisualStudio
MANOJ KUMAR SHARMA
Platform & Developer Evangelist
mailme@manojkumarsharma.com
http://www.linkedin.com/in/contact4manoj
Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved.
This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission.
25
Thank You!

More Related Content

PDF
Introduction of c# day3
PPTX
Adapter design-pattern2015
ZIP
Adapter Design Pattern
PDF
Introduction to functional programming
PPT
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team
PPT
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA
PDF
OO Design and Design Patterns in C++
DOCX
C++ & Design Patterns Quicky
Introduction of c# day3
Adapter design-pattern2015
Adapter Design Pattern
Introduction to functional programming
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA
OO Design and Design Patterns in C++
C++ & Design Patterns Quicky

What's hot (18)

PDF
PHP, Java EE & .NET Comparison
PPTX
Comparison of Programming Platforms
ODP
Let's talk about certification: SCJA
PPTX
PPS
Dacj 2-2 a
PPTX
Java Fundamentals in Mule
PPT
Java for C++ programers
PPTX
Curso de Programación Java Básico
PDF
java training institute course class indore
PPTX
Introduction to flutter's basic concepts
DOC
Java questions and answers jan bask.net
PDF
Java language is different from other programming languages, How?
PPTX
Behaviour Driven Development V 0.1
PPTX
6 Weeks Summer Training on Java By SSDN Technologies
PDF
Tl Recruit360 Features V1.3
PDF
PHP Interview Questions and Answers | Edureka
PHP, Java EE & .NET Comparison
Comparison of Programming Platforms
Let's talk about certification: SCJA
Dacj 2-2 a
Java Fundamentals in Mule
Java for C++ programers
Curso de Programación Java Básico
java training institute course class indore
Introduction to flutter's basic concepts
Java questions and answers jan bask.net
Java language is different from other programming languages, How?
Behaviour Driven Development V 0.1
6 Weeks Summer Training on Java By SSDN Technologies
Tl Recruit360 Features V1.3
PHP Interview Questions and Answers | Edureka
Ad

Similar to Whats Newi in C# 8.0 (20)

PPTX
Advanced angular
PDF
Review Paper on Online Java Compiler
PDF
How to Design a Good API and Why it Matters.pdf
PDF
How to design good api
PDF
IRJET- Build a Secure Web based Code Editor for C Programming Language
PPTX
Max’s Birthday Adventure: #19 Kochi : Anypoint Code Builder
PPTX
PDF
Practices and tools for building better API (JFall 2013)
PDF
Practices and tools for building better APIs
PPTX
PL-400T00A-ENU-PowerPoint_03.pptx - Power Platform
PPTX
Presentation
PDF
Trouble with Performance Debugging? Not Anymore with Choreo, the AI-Assisted ...
PDF
How To Design A Good A P I And Why It Matters G O O G L E
PDF
Faridabad MuleSoft Meetup Group (1).pdf
PDF
C For Beginners Crash Course Master C Programming Fast And Easy Today 1st Edi...
PDF
How to become a Rational Developer for IBM i Power User
PDF
Keynoteof A P I
PPTX
Technical trainning.pptx
PPT
C# Fundamental
Advanced angular
Review Paper on Online Java Compiler
How to Design a Good API and Why it Matters.pdf
How to design good api
IRJET- Build a Secure Web based Code Editor for C Programming Language
Max’s Birthday Adventure: #19 Kochi : Anypoint Code Builder
Practices and tools for building better API (JFall 2013)
Practices and tools for building better APIs
PL-400T00A-ENU-PowerPoint_03.pptx - Power Platform
Presentation
Trouble with Performance Debugging? Not Anymore with Choreo, the AI-Assisted ...
How To Design A Good A P I And Why It Matters G O O G L E
Faridabad MuleSoft Meetup Group (1).pdf
C For Beginners Crash Course Master C Programming Fast And Easy Today 1st Edi...
How to become a Rational Developer for IBM i Power User
Keynoteof A P I
Technical trainning.pptx
C# Fundamental
Ad

Recently uploaded (20)

PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Cost to Outsource Software Development in 2025
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Download FL Studio Crack Latest version 2025 ?
PDF
iTop VPN Crack Latest Version Full Key 2025
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Website Design Services for Small Businesses.pdf
PPTX
assetexplorer- product-overview - presentation
Designing Intelligence for the Shop Floor.pdf
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
wealthsignaloriginal-com-DS-text-... (1).pdf
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Operating system designcfffgfgggggggvggggggggg
Cost to Outsource Software Development in 2025
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Download FL Studio Crack Latest version 2025 ?
iTop VPN Crack Latest Version Full Key 2025
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Salesforce Agentforce AI Implementation.pdf
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Advanced SystemCare Ultimate Crack + Portable (2025)
Digital Systems & Binary Numbers (comprehensive )
Website Design Services for Small Businesses.pdf
assetexplorer- product-overview - presentation

Whats Newi in C# 8.0

  • 1. MANOJ KUMAR SHARMA Platform & Developer Evangelist mailme@manojkumarsharma.com http://www.linkedin.com/in/contact4manoj Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. 1 What’s new in C# 8.0
  • 2. 2 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Introduction What’s new in C# 8.0
  • 3. 3 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. How to check available C# versions? • Open Developer Command Prompt  csc -langversion:? Developer Command Prompt for VS2019Developer Command Prompt for VS2019
  • 4. 4 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Default Language Version The Compiler determines the DEFAULT Language Version. Target framework Version Default C# language version .NET Core 3.x C# 8.0 .NET Core 2.x C# 7.3 .NET Standard 2.1 C# 8.0 .NET Standard 2.0 C# 7.3 .NET Standard 1.x C# 7.3 .NET Framework all C# 7.3
  • 5. 5 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. How to use C# 8.0 • To override the DEFAULT version to use C# 8.0: • Edit the .csproj file ... <PropertyGroup> ... <OutputType>Exe</OutputType> ... <TargetFrameworkVersion>v4.8</TargetFrameworkVersion> <LangVersion>8.0</LangVersion> ... </PropertyGroup> ... .csproj.csproj
  • 6. 6 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Support modern cloud scenarios • Async enumerables • More patterns in more places • Default interface members • Indices and Ranges
  • 7. 7 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Increase your productivity • Using statement • Static local functions • Readonly members • Null coalescing assignment • Unmanaged constraint • Interpolated verbatim strings
  • 8. 8 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. What’s new in C# 8.0 • 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 • Indices and ranges • Null-coalescing assignment • Unmanaged constructed types • stackalloc in nested expressions • Enhancement of interpolated verbatim strings
  • 9. 9 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Some of them…. What’s new in C# 8.0
  • 10. 10 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Readonly members • Add the readonly modifier to any structure member. • Indicates that the member does not modify state. • It's more granular than applying the readonly modifier to a struct declaration. • This feature lets you specify your design intent so the compiler can enforce it, and make optimizations based on that intent. cs8_con_ReadonlyMembers
  • 11. 11 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Default Interface Methods • Used to define default method implementations to members implementing the Interface. • Change Interfaces without breaking changes • Reusability of methods in independent classes • Based on Java’s Default Methods • Extensions are now possible! • Alternative to Extension Methods • Runtime polymorphism • Allowed Modifiers: private, protected, internal, public, virtual, abstract, override, sealed, static, external cs8_con_DefaultInterfaceMethods
  • 12. 12 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Ranges and Indices • Based upon two new Types: • System.Index • Represents an index for a sequence of objects in collection • The index from end operator ( ^ ) (known as “hat” operator) • Works on Countable Types having Length / Count and an Instance Indexer with int • System.Range • Represents a sub-range of a sequence of objects in the collection • The range operator ( .. ) specifies the start and end of a range as its operands • Works on Countable Types having Length / Count and Slice() method with two int • NOTE: • Ranges is not supported by List<T> • The element selection is: • 0-based if you are counting from the beginning, and • 1-based if you are counting from the end. cs8_con_RangesAndIndices
  • 13. 13 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Null Reference Types • Sir Charles Antony Richard Hoare • Inventor of QuickSort Algorithm in 1959/1960 • In 2009 at QCon, London, apologized for inventing Null Reference • The most common .NET Exception – NullReferenceException I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement….
  • 14. 14 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Null Reference Types • In C# 5.0: Coalescing Operator ( ?? ) was introduced to provide Default Values for null decimal basicSalary; private void AddBonus(decimal? percent) { // Would throw NullReferenceException if percent is null! basicSalary += (basicSalary * percent.Value); // Solution: Check for null, before consumption basicSalary += percent.HasValue ? (basicSalary * percent.Value) : 0M; // Solution: C# 5.0 approach basicSalary += basicSalary * (percent ?? 0M); } Employee.csEmployee.cs
  • 15. 15 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Null Reference Types • In C# 6.0: Null-Check Operator / Null-Safe Operator ( ?. ) simplified code • NOTE: ( ?. ) returns a nullable value See https://enterprisecraftsmanship.com/posts/3-misuses-of-operator-in-c-6/ for more information to understand when and how to use efficiently. int? productsCount = products?.Length; .cs.cs
  • 16. 16 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. How to use Nullable Reference Types • To enable Nullable Reference Types in C# 8.0 Project: ... <PropertyGroup> ... <OutputType>Exe</OutputType> ... <TargetFrameworkVersion>v4.8</TargetFrameworkVersion> <LangVersion>8.0</LangVersion> <Nullable>enable</Nullable> ... </PropertyGroup> ... .csproj.csproj
  • 17. 17 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. How to use Nullable Reference Types • To enable at the file level: • To disable at the file level: #nullable enable using System; ... .cs.cs #nullable disable using System; ... .cs.cs
  • 18. 18 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Nullable Reference Types • To help mitigate NullReferenceException with Compiler Warnings: ( ?? ) Null-Coalescing Operator C# 5.0 ( ! ) Postfix Unary Null-Forgiving Operator C# 8.0 ( ??= ) Null-Coalescing Assignment Operator C# 8.0 ( ?. ) Null-Coalescing Conditional Operator a.k.a. Null-Safe Operator C# 6.0 • Helps to find bugs; Flow analysis tracks nullable reference variables See also: • https://docs.microsoft.com/en-us/dotnet/csharp/nullable-attributes • https://www.meziantou.net/csharp-8-nullable-reference-types.htm • https://docs.microsoft.com/en-us/dotnet/csharp/nullable-attributes • https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/nullable-reference-types cs8_con_NullableReferenceTypes
  • 19. 19 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Nullable Reference Types • Recommended Guidelines for adoption: • Library developers – Nullable adoption phase before .NET 5 • App developers – nullability on your own pace • Annotate new APIs • Do not remove argument validation • Parameter is non-nullable if parameters are checked (ArgumentNullException) • Parameter is nullable if documented to accept null • Prefer nullable over non-nullable with disagreements
  • 20. 20 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Async Streams • so far: async / await returns a result • Async streams extends async / await stream of results • Asynchronous data sources from the consumer to be controlled • Alternative to Reactive Extensions (Rx) for .NET System.Reactive (https://github.com/dotnet/reactive) A library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators USE CASE: • Streaming from Server to Client • Streaming from Client to Server
  • 21. 21 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Async Streams public interface IAsyncEnumerator<[NullableAttribute(2)] out T> : IAsyncDisposable { T Current { get; } ValueTask<bool> MoveNextAsync(); } System.Collections.Generic.IAsyncEnumerator.csSystem.Collections.Generic.IAsyncEnumerator.cs public interface IAsyncEnumerable<out T> { IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default); } System.Collections.Generic.IAsyncEnumerable.csSystem.Collections.Generic.IAsyncEnumerable.cs public interface IAsyncDisposable { ValueTask DisposeAsync(); } System.IAsyncDisposable.csSystem.IAsyncDisposable.cs cs8_con_AsyncStreams
  • 22. 22 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Summary What’s new in C# 8.0
  • 23. 23 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. C# 8.0 Safe Nullable and non-nullable reference types help you write safer code. Declare your intent more clearly. Modern Async streams for modern workloads like Cloud & IoT communication. Easily work with cloud scale datasets using Indexes and Ranges. Productive Write less code using Patterns. Protect data with readonly members. Improved using statements for resource management.
  • 24. 24 Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. Further Learning… • What’s New in C# 8.0 https://aka.ms/new-csharp • .NET CONF 2019 official website https://www.dotnetconf.net • Videos on Youtube: http://bit.ly/bdotnetconf2019 • All .NET CONF 2019 Materials: https://github.com/dotnet-presentations/dotnetconf2019 • To edit “Edit Project File” and other useful extensions in VS2019 Power Commands for Visual Studio – Microsoft DevLabs https://marketplace.visualstudio.com/items?itemName=VisualSt udioPlatformTeam.PowerCommandsforVisualStudio
  • 25. MANOJ KUMAR SHARMA Platform & Developer Evangelist mailme@manojkumarsharma.com http://www.linkedin.com/in/contact4manoj Copyright © 2019-2020 Manoj Kumar Sharma. All rights reserved. This presentation is for training purposes only, and cannot be reproduced in any form without an express written permission. 25 Thank You!