Whats Newi in C# 8.0

Manoj Sharma
Manoj SharmaFreelance Trainer / Software Consultant at NONE
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!
1 of 25

Recommended

Introduction of c# day3 by
Introduction of c# day3Introduction of c# day3
Introduction of c# day3Arun Kumar Singh
771 views20 slides
Adapter design-pattern2015 by
Adapter design-pattern2015Adapter design-pattern2015
Adapter design-pattern2015Vic Tarchenko
1.3K views22 slides
Adapter Design Pattern by
Adapter Design PatternAdapter Design Pattern
Adapter Design Patternguy_davis
9.9K views48 slides
Introduction to functional programming by
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programmingThang Mai
934 views26 slides
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team by
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC TeamAOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC TeamThuy_Dang
1.3K views39 slides
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA by
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEAScala - By Luu Thanh Thuy CWI team from eXo Platform SEA
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEAThuy_Dang
1K views24 slides

More Related Content

What's hot

PHP, Java EE & .NET Comparison by
PHP, Java EE & .NET ComparisonPHP, Java EE & .NET Comparison
PHP, Java EE & .NET ComparisonHaim Michael
6.9K views34 slides
Comparison of Programming Platforms by
Comparison of Programming PlatformsComparison of Programming Platforms
Comparison of Programming PlatformsAnup Hariharan Nair
9.5K views27 slides
Let's talk about certification: SCJA by
Let's talk about certification: SCJALet's talk about certification: SCJA
Let's talk about certification: SCJAJosé Maria Silveira Neto
1.7K views33 slides
Java Tokens by
Java  TokensJava  Tokens
Java TokensMadishetty Prathibha
2.4K views21 slides
Dacj 2-2 a by
Dacj 2-2 aDacj 2-2 a
Dacj 2-2 aNiit Care
645 views49 slides
Java Fundamentals in Mule by
Java Fundamentals in MuleJava Fundamentals in Mule
Java Fundamentals in MuleAnand kalla
246 views22 slides

What's hot(19)

PHP, Java EE & .NET Comparison by Haim Michael
PHP, Java EE & .NET ComparisonPHP, Java EE & .NET Comparison
PHP, Java EE & .NET Comparison
Haim Michael6.9K views
Dacj 2-2 a by Niit Care
Dacj 2-2 aDacj 2-2 a
Dacj 2-2 a
Niit Care645 views
Java Fundamentals in Mule by Anand kalla
Java Fundamentals in MuleJava Fundamentals in Mule
Java Fundamentals in Mule
Anand kalla246 views
java training institute course class indore by Future Multimedia
java training institute course class indorejava training institute course class indore
java training institute course class indore
Future Multimedia204 views
java token by Jadavsejal
java tokenjava token
java token
Jadavsejal3.1K views
Java language is different from other programming languages, How? by Gyanguide1
Java language is different  from other programming languages, How?Java language is different  from other programming languages, How?
Java language is different from other programming languages, How?
Gyanguide12.4K views
Behaviour Driven Development V 0.1 by willmation
Behaviour Driven Development V 0.1Behaviour Driven Development V 0.1
Behaviour Driven Development V 0.1
willmation223 views
6 Weeks Summer Training on Java By SSDN Technologies by David Son
6 Weeks Summer Training on Java By SSDN Technologies6 Weeks Summer Training on Java By SSDN Technologies
6 Weeks Summer Training on Java By SSDN Technologies
David Son1.8K views
Tl Recruit360 Features V1.3 by ryanpmay
Tl Recruit360 Features V1.3Tl Recruit360 Features V1.3
Tl Recruit360 Features V1.3
ryanpmay481 views
PHP Interview Questions and Answers | Edureka by Edureka!
PHP Interview Questions and Answers | EdurekaPHP Interview Questions and Answers | Edureka
PHP Interview Questions and Answers | Edureka
Edureka!248 views

Similar to Whats Newi in C# 8.0

Advanced angular by
Advanced angularAdvanced angular
Advanced angularSumit Kumar Rakshit
67 views61 slides
Review Paper on Online Java Compiler by
Review Paper on Online Java CompilerReview Paper on Online Java Compiler
Review Paper on Online Java CompilerIRJET Journal
54 views5 slides
How to design good api by
How to design good apiHow to design good api
How to design good apiOsama Shakir
331 views49 slides
IRJET- Build a Secure Web based Code Editor for C Programming Language by
IRJET-  	  Build a Secure Web based Code Editor for C Programming LanguageIRJET-  	  Build a Secure Web based Code Editor for C Programming Language
IRJET- Build a Secure Web based Code Editor for C Programming LanguageIRJET Journal
11 views3 slides
Anagha by
AnaghaAnagha
AnaghaAnaghabalakrishnan
206 views21 slides

Similar to Whats Newi in C# 8.0(20)

Review Paper on Online Java Compiler by IRJET Journal
Review Paper on Online Java CompilerReview Paper on Online Java Compiler
Review Paper on Online Java Compiler
IRJET Journal54 views
How to design good api by Osama Shakir
How to design good apiHow to design good api
How to design good api
Osama Shakir331 views
IRJET- Build a Secure Web based Code Editor for C Programming Language by IRJET Journal
IRJET-  	  Build a Secure Web based Code Editor for C Programming LanguageIRJET-  	  Build a Secure Web based Code Editor for C Programming Language
IRJET- Build a Secure Web based Code Editor for C Programming Language
IRJET Journal11 views
Practices and tools for building better API (JFall 2013) by Peter Hendriks
Practices and tools for building better API (JFall 2013)Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)
Peter Hendriks4.5K views
Practices and tools for building better APIs by NLJUG
Practices and tools for building better APIsPractices and tools for building better APIs
Practices and tools for building better APIs
NLJUG569 views
Presentation by bhasula
PresentationPresentation
Presentation
bhasula263 views
Trouble with Performance Debugging? Not Anymore with Choreo, the AI-Assisted ... by WSO2
Trouble with Performance Debugging? Not Anymore with Choreo, the AI-Assisted ...Trouble with Performance Debugging? Not Anymore with Choreo, the AI-Assisted ...
Trouble with Performance Debugging? Not Anymore with Choreo, the AI-Assisted ...
WSO297 views
How To Design A Good A P I And Why It Matters G O O G L E by guestbe92f4
How To Design A Good  A P I And Why It Matters    G O O G L EHow To Design A Good  A P I And Why It Matters    G O O G L E
How To Design A Good A P I And Why It Matters G O O G L E
guestbe92f431.6K views
Technical trainning.pptx by SanuSan3
Technical trainning.pptxTechnical trainning.pptx
Technical trainning.pptx
SanuSan32 views
C# c# for beginners crash course master c# programming fast and easy today by Afonso Macedo
C# c# for beginners crash course master c# programming fast and easy todayC# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy today
Afonso Macedo402 views
Content Strategy and Developer Engagement for DevPortals by Axway
Content Strategy and Developer Engagement for DevPortalsContent Strategy and Developer Engagement for DevPortals
Content Strategy and Developer Engagement for DevPortals
Axway321 views
Indy meetup#7 effective unit-testing-mule by ikram_ahamed
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-mule
ikram_ahamed375 views
Mule ESB Intro by Noga Manela
Mule ESB IntroMule ESB Intro
Mule ESB Intro
Noga Manela1.2K views

Recently uploaded

DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... by
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...Deltares
9 views24 slides
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker by
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - ParkerDSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - ParkerDeltares
9 views16 slides
SAP FOR TYRE INDUSTRY.pdf by
SAP FOR TYRE INDUSTRY.pdfSAP FOR TYRE INDUSTRY.pdf
SAP FOR TYRE INDUSTRY.pdfVirendra Rai, PMP
23 views3 slides
El Arte de lo Possible by
El Arte de lo PossibleEl Arte de lo Possible
El Arte de lo PossibleNeo4j
38 views35 slides
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... by
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...Deltares
16 views12 slides
Upgrading Incident Management with Icinga - Icinga Camp Milan 2023 by
Upgrading Incident Management with Icinga - Icinga Camp Milan 2023Upgrading Incident Management with Icinga - Icinga Camp Milan 2023
Upgrading Incident Management with Icinga - Icinga Camp Milan 2023Icinga
38 views17 slides

Recently uploaded(20)

DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... by Deltares
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
Deltares9 views
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker by Deltares
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - ParkerDSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker
Deltares9 views
El Arte de lo Possible by Neo4j
El Arte de lo PossibleEl Arte de lo Possible
El Arte de lo Possible
Neo4j38 views
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
Deltares16 views
Upgrading Incident Management with Icinga - Icinga Camp Milan 2023 by Icinga
Upgrading Incident Management with Icinga - Icinga Camp Milan 2023Upgrading Incident Management with Icinga - Icinga Camp Milan 2023
Upgrading Incident Management with Icinga - Icinga Camp Milan 2023
Icinga38 views
Software evolution understanding: Automatic extraction of software identifier... by Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko... by Deltares
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
Deltares11 views
SUGCON ANZ Presentation V2.1 Final.pptx by Jack Spektor
SUGCON ANZ Presentation V2.1 Final.pptxSUGCON ANZ Presentation V2.1 Final.pptx
SUGCON ANZ Presentation V2.1 Final.pptx
Jack Spektor22 views
Cycleops - Automate deployments on top of bare metal.pptx by Thanassis Parathyras
Cycleops - Automate deployments on top of bare metal.pptxCycleops - Automate deployments on top of bare metal.pptx
Cycleops - Automate deployments on top of bare metal.pptx
DSD-INT 2023 HydroMT model building and river-coast coupling in Python - Bove... by Deltares
DSD-INT 2023 HydroMT model building and river-coast coupling in Python - Bove...DSD-INT 2023 HydroMT model building and river-coast coupling in Python - Bove...
DSD-INT 2023 HydroMT model building and river-coast coupling in Python - Bove...
Deltares17 views
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea... by Safe Software
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...
Safe Software412 views
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI... by Marc Müller
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Marc Müller36 views
Copilot Prompting Toolkit_All Resources.pdf by Riccardo Zamana
Copilot Prompting Toolkit_All Resources.pdfCopilot Prompting Toolkit_All Resources.pdf
Copilot Prompting Toolkit_All Resources.pdf
Riccardo Zamana6 views
A first look at MariaDB 11.x features and ideas on how to use them by Federico Razzoli
A first look at MariaDB 11.x features and ideas on how to use themA first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use them
Federico Razzoli45 views
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -... by Deltares
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
Deltares6 views
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports by Ra'Fat Al-Msie'deen
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
Neo4j y GenAI by Neo4j
Neo4j y GenAI Neo4j y GenAI
Neo4j y GenAI
Neo4j42 views
What Can Employee Monitoring Software Do?​ by wAnywhere
What Can Employee Monitoring Software Do?​What Can Employee Monitoring Software Do?​
What Can Employee Monitoring Software Do?​
wAnywhere21 views

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!