SlideShare a Scribd company logo
1 of 36
Download to read offline
C# 7 Development
Fisnik Doko
2
Presenter
C# 7 Development
Fisnik Doko
• Microsoft Certified Trainer
• MCSD, MCPD, MCSE, MCSA, MCTS, MCP, MCT
• Senior Software Developer / Architect – NBRM
• Speaker
• Consultant
Agenda
C# Development
C# and versions
.01
.02
C# 7.0, 7.1, 7.2, 7.3
.03
C# 8.0
4
C# Versions
Lorem
5
Stack Overflow Developer Survey
6
C# 7 – How do I get it
• Visual Studio 2017
• Nuget package Microsoft.Net.Compilers
• Roslyn compiler from GitHub
• Online https://sharplab.io/
7
Configure Build for language version
8
New Features in C# 7.0, 7.1, 7.2, 7.3
C# 7.0 C# 7.1 C# 7.2 C# 7.3
• Binary Literals
• Digit Separators
• Local Functions
• Out Variables
Tuples
• Deconstruction
• Discards
• Ref Returns & Locals
• Pattern Matching
• Expression Bodied
Members
• Throw Expression
• Async main
• Default literal
expressions
• Inferred tuple element
names
• Reference assembly
generation
• Digital Separator after
Base Specifier
• Non-trailing named
arguments
• Leading underscores
in numeric literals
• private protected
access modifier
• Reference semantics
with value types
• Ref Local
Reassignment
• Overload resolution
• Attributes on backing
fields
• Tuple comparison
• Expression variables
in initializers
VS 2017
March 2017
VS 2017 V15.3
August 2017
VS 2017 v15.5
January 2018
VS 2017 v15.7
May 2018
9
Defining out Parameters Inline
• C# 7 allows you to declare out parameters inline, during a method
call
• Cleaner syntax - no need to pre-declare before the method call
• Also ensures you can't use the variable before the method call
So the following examples are equivalent:
C# 7
int prod, quot;
MethodOut(num1, num2, out prod, out quot);
Console.WriteLine("After MethodOut, prod={0}, quot={1}", prod, quot);
MethodOut(num1, num2, out int p, out int q);
Console.WriteLine("After MethodOut, using C# 7 syntax, p={0}, q={1}", p, q);
10
Numeric Literals
• Binary literals
• Allows you to use binary for integer literals (use the prefix 0b)
• Useful if you're doing a lot of bit manipulation
• Digit separators
• Allows you to embed _ anywhere in numeric literals (integer,
long, decimal, double)
• Makes it easier to read big numbers
C# 7
int six = 0b110;
int thirtyFive = 0b100011;
int sixtyFive = 0b1000001;
long idealAnnualSalary = 123_456____789;
double electronicCharge = 1.602_176_62E-19;
11
Expression-Body Members in C# 6
• C# 6 introduced the idea of "expression-bodied members“
• Members that comprises a simple expression (via lambda syntax)
• C# 6 supports the following expression-bodied members:
• Methods
• Read-only properties
C# 6
class Employee
{
public string Name { get; set; }
public double Salary { get; set; }
public void PayRaise(double amount) => Salary += amount; // Method
public double TaxPayable => Salary * 0.25; // Read-only property
…
12
Expression-Body Members in C# 7
• C# 7 also allows expression-bodied members for:
• Constructors
• Finalizers (i.e. destructors)
• Property getters and setters
C# 7
class Contact
{
private string name;
public Contact(string name) => this.name = name; // Constructor
~Contact() => Console.WriteLine($"Finalized {name}"); // Finalizer
public string Name
{
get => name; // Property getter
set => name = value ?? "Anonymous"; // Property setter
}
13
Exception Filters
• C# 6 and above allows you to define exception filters
• Append a when boolean expression to a catch clause
• If the expression is true, the catch block is run (otherwise the
exception keeps going)
C# 6
try
{
…
}
catch (SomeException ex) when (MyFilterFuncThatReturnsBoolean(ex))
{
…
}
14
C# 7 Throw Expressions
• Prior to C# 7, you couldn't throw an exception in the middle of
another expression
• So the following statements won't compile prior to C# 7
• C# 7 now supports "throw expressions", so you use throw an
exception in the middle of an another expression
C# 7
string variable1 = SomeFuncToCalcValue() ?? throw new SomeException("Error!");
string variable2 = (someTest) ? "hooray!" : throw new SomeException("Error!");
String Name
{
get => name;
set => name = value ?? throw new SomeException("Error!");
}
15
Tuples
• Tuples are a new feature in C# 7
• A tuple is like an anonymous bag of related data fields
• The data fields can be of different types
• Use of tuples:
• Handy if you want to group some data fields together, without
the overhead of defining a separate class/struct
• E.g. returning multiple values from a function
• Note:
• Prior to .NET 4.7, you must install the System.ValueTuple
NuGet package in your project if you want to use tuples
C# 7
16
Tuples Syntax
• You can assign a tuple to a var variable
• By default, the tuple fields are named Item1, Item2, etc
• You can specify field names when you create a tuple
• Alternatively you can specify field names by assigning the tuple to
named variables enclosed in ()
C# 7
var personA = ("Matthew", 21);
Console.WriteLine($"{personA.Item1} is {personA.Item2} years old");
var personB = (Name: "Mark", Age: 22);
Console.WriteLine($"{personB.Name} is {personB.Age} years old");
(string name, int age) = ("Luke", 23);
Console.WriteLine($"{name} is {age} years old");
17
Using Tuples in Functions
• A function can return multiple values via a tuple
• Client code can call the function and use the tuple as follows:
• Alternatively, client code can unpick the tuple fields like this:
C# 7
private static (double avg, int count) GetStats(params int[] nums)
{
..
return (sum/i, i);
}
var stats = GetStats(10, 20, 30, 40);
Console.WriteLine($"Average is {stats.avg}, count of items is {stats.count}");
(double a, int n) = GetStats(50, 60, 70, 80);
Console.WriteLine($"Average is {a}, count of items is {n}");
18
Deconstruct
• You can define a Deconstruct() method in classes and structs, to
extract multiple values from an object
• Client code can assign an object directly to a tuple
C# 7
public class Product
{
…
public void Deconstruct(out string description, out double unitPrice)
{
description = this.Description;
unitPrice = this.UnitPrice;
}
}
Product productA = new Product("The Good Book", 12.99);
(string desc, double price) = productA;
Console.WriteLine($"{desc} costs {price:c}");
19
Discards
• Discards are temporary, write-only variables used in assignments
when you don't care about the value assigned
• Discards can reduce memory allocations
• Variable is a discard by assigning it the underscore (_) as its name
In C# 7.0, discards are supported in assignments in the following
contexts:
• Tuple and object deconstruction
• Calls to methods with out parameters
C# 7
(_, _, area) = city.GetCityInformation(cityName);
Result(a, b, out int sum, out int _, out int _, out int _, out int _);
if (int.TryParse(ReadLine(), out int _))
20
Local Functions
• C# 7 allows you to define functions inside other functions
• Local functions may use variables from the enclosing scope
• Have all features of a regular method except that local functions
cannot be static in nature
• Scenario:
• You're implementing function A, and it's starting to get messy
• You can move some complexity into another function B
• You can put function B inside function A, if it's only needed there
• Motivation:
• Reduce the complexity of the class as a whole, also reducing memory
allocation
C# 7
21
Defining ref Return Values
• C# 7 allows a function to return a reference
• Decorate the function return type with ref
• Also decorate all return values with ref
C# 7
static ref double FindMinElem(double[] arr)
{
int minpos = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] < arr[minpos])
minpos = i;
}
return ref arr[minpos];
}
22
Defining ref Local Variables
• When you call a function that returns a reference…
• If you assign the result to a variable, it just holds a copy
• If you assign the result to a ref variable, it holds a reference
C# 7
double [] array = { 10.5, 3.14, 99.9, 64.1 };
double minElem = FindMinElem(array); // minElem holds a copy of the element value.
Console.WriteLine($"nValue of min element: {minElem}");
minElem = 1.2345; // Modifies local variable, not the array element.
Console.WriteLine($"Value of element [1]: {array[1]}");
double [] array = { 10.5, 3.14, 99.9, 64.1 };
ref double refMinElem = ref FindMinElem(array); // refMinElem refers to element.
Console.WriteLine($"nValue of min element: {refMinElem}");
refMinElem = 1.2345; // Modifies the actual array element now.
Console.WriteLine($"Value of element [1]: {array[1]}");
23
Pattern Matching via "is" Expressions
• In C# 7, you can use is expressions for pattern matching in if
statements
• You can test if a value equals some constant
• You can also test if a value is some type (if so, it is automatically cast
and assigned to a fresh variable of that type)
C# 7
static private int ParseValue(object value)
{
if (value is null) return 0;
else if (value is int iValue) return iValue;
else if (value is double dValue) return (int)Math.Round(dValue);
else if (value is string strValue) return int.Parse(strValue);
else return 0;
}
24
Pattern Matching in switch Statements
• C# 7 also allows the following in a switch statement:
• In a case branch, specify the type you want to test against
• You can use a when clause to refine a case test
• You can mix-and-match constants and types in case branches
C# 7
static private int ParseValue(object value)
{
if (value is null) return 0;
else if (value is int iValue) return iValue;
else if (value is double dValue) return (int)Math.Round(dValue);
else if (value is string strValue) return int.Parse(strValue);
else return 0;
}
25
Generalized async return types
• In C# 7, there is an inbuilt value type ValueTask <T> which can be
used instead of Task<T>
• NuGet package System.Threading.Tasks.Extensions
• Not replace Task, helps to reduce the number of allocation if is
invoked frequently
C# 7
static void Main(string[] args)
{
int x= GetLargestPrimeNumber(20).Result;
.
}
private static async ValueTask<int> GetLargestPrimeNumber(int maxNumber)
{
...
26
Async Main Methods
• Allow await to be used in an application's Main method by allowing
the entry point to return Task / Task<int> and be marked async
Extends the list of allowed entry points to include:
• static async Task Main()
• static Task Main()
• static async Task<int> Main()
• static Task<int> Main()
• static async Task Main(string[])
• static Task Main(string[])
• static async Task<int> Main(string[])
• static Task<int> Main(string[])
C# 7.1
27
Async Main Methods
C# 7.1
28
The default Literal
• The target-typed default feature is a shorter form variation of the
default(T) operator, which allows the type to be omitted
• Its type is inferred by target-typing instead
C# 7.1
// from C# 7.1
int a = default;
bool b = default;
string c = default;
int? d = default;
Action<int, bool> action = default;
Predicate<string> predicate = default;
List<string> list = default;
int a = default(int);
bool b = default(bool);
string c = default(string);
int? d = default(int?);
Action<int, bool> action = default(Action<int, bool>);
Predicate<string> predicate = default(Predicate<string>);
List<string> list = default(List<string>);
29
Inferred Tuple Element Names
• Infer tuple names from parameters
• Similar to anonymous types
C# 7.1
30
C# 7.2
• Digital Separator after Base Specifier
• int binaryValue = 0b_0101_0101;
• Non-trailing Named Arguments
• Method calls may now use named arguments that precede positional
arguments when those named arguments are in the correct positions
C# 7.2
31
C# 7.2
• Private Protected access modifier
• members will only be visible to
subclasses in the same assembly
• Ref Conditional Expression
C# 7.2
ref var firstItem = ref (emptyArray.Length > 0 ? ref emptyArray[0] : ref nonEmptyArray[0]);
32
C# 7.3
• Ref Local Reassignment
• Now, ref locals may be reassigned to refer to different instances after
being initialized
• Additional Generic Constraints
• You can now specify the type System.Enum or System.Delegate as
base class constraints for a type parameter
C# 7.3
ref var max = ref b;
if (a > b)
{
max = ref a;
}
public static TDelegate Combine<TDelegate>(...) where TDelegate : Delegate
public static string GetDescription<TEnum>(...) where TEnum : Enum
33
C# 7.3
• Attributes Targeting Fields of Auto-Implemented Properties
• Expression Variables in Initializers
• out variable declarations has been extended to include field initializers,
property initializers, constructor initializers
• Equality Operators for Value Tuples
C# 7.3
[field: NonSerialized]
public double X { get; set; }
34
C# 8.0 planned features
• Asynchronous Streams / Sequences
• Asynchronous Dispose
• Extension everything
• Adding support for properties, static methods
• Records
• Lightweight class only with fields
C# 8
public class Sword(int Damage, int Durability);
35
C# 8.0 planned features
• Ranges
• new syntax for expressing a range of values
• Nullable reference types
• Default interface implementations
C# 8
var range = 1..5;
…
foreach (var index in min..max)
{ }
String? s = null;
Thank You
Questions?

More Related Content

What's hot

Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Abu Saleh
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - CJussi Pohjolainen
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5thConnex
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++guestf0562b
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Andreas Dewes
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...Francesco Casalegno
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language Mohamed Loey
 
(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-stringsNico Ludwig
 
Replace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeReplace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeAkira Takahashi
 

What's hot (20)

Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - C
 
C++11
C++11C++11
C++11
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 
C++ Templates 2
C++ Templates 2C++ Templates 2
C++ Templates 2
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
 
C++ book
C++ bookC++ book
C++ book
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
Pointers
PointersPointers
Pointers
 
Oop l2
Oop l2Oop l2
Oop l2
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings
 
Replace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeReplace OutputIterator and Extend Range
Replace OutputIterator and Extend Range
 

Similar to C# 7 development

Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#ANURAG SINGH
 
What’s new in .NET
What’s new in .NETWhat’s new in .NET
What’s new in .NETDoommaker
 
C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)Christian Nagel
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginnersophoeutsen2
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11Jadavsejal
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#Hawkman Academy
 
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?", Yevhen TatarynovFwdays
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageAzilen Technologies Pvt. Ltd.
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project AnalyzedPVS-Studio
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxvanshhans21102005
 

Similar to C# 7 development (20)

Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
What’s new in .NET
What’s new in .NETWhat’s new in .NET
What’s new in .NET
 
C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)
 
C#unit4
C#unit4C#unit4
C#unit4
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
C# - What's Next?
C# - What's Next?C# - What's Next?
C# - What's Next?
 
Aspdot
AspdotAspdot
Aspdot
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11
 
Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
Csharp
CsharpCsharp
Csharp
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
 
Return of c++
Return of c++Return of c++
Return of c++
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project Analyzed
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 

More from Fisnik Doko

Developing Solutions for Azure - Best Practices
Developing Solutions for Azure - Best PracticesDeveloping Solutions for Azure - Best Practices
Developing Solutions for Azure - Best PracticesFisnik Doko
 
Building Scalable Applications with Microsoft Azure
Building Scalable Applications with Microsoft AzureBuilding Scalable Applications with Microsoft Azure
Building Scalable Applications with Microsoft AzureFisnik Doko
 
Power BI measure and visualize project success
Power BI measure and visualize project successPower BI measure and visualize project success
Power BI measure and visualize project successFisnik Doko
 
Microsoft's modern technologies
Microsoft's modern technologiesMicrosoft's modern technologies
Microsoft's modern technologiesFisnik Doko
 
Predictive Analysis using Microsoft SQL Server R Services
Predictive Analysis using Microsoft SQL Server R ServicesPredictive Analysis using Microsoft SQL Server R Services
Predictive Analysis using Microsoft SQL Server R ServicesFisnik Doko
 
Analyses and processing of big data in financial services
Analyses and processing of big data in financial servicesAnalyses and processing of big data in financial services
Analyses and processing of big data in financial servicesFisnik Doko
 
HTML5 features & JavaScript APIs
HTML5 features & JavaScript APIsHTML5 features & JavaScript APIs
HTML5 features & JavaScript APIsFisnik Doko
 

More from Fisnik Doko (7)

Developing Solutions for Azure - Best Practices
Developing Solutions for Azure - Best PracticesDeveloping Solutions for Azure - Best Practices
Developing Solutions for Azure - Best Practices
 
Building Scalable Applications with Microsoft Azure
Building Scalable Applications with Microsoft AzureBuilding Scalable Applications with Microsoft Azure
Building Scalable Applications with Microsoft Azure
 
Power BI measure and visualize project success
Power BI measure and visualize project successPower BI measure and visualize project success
Power BI measure and visualize project success
 
Microsoft's modern technologies
Microsoft's modern technologiesMicrosoft's modern technologies
Microsoft's modern technologies
 
Predictive Analysis using Microsoft SQL Server R Services
Predictive Analysis using Microsoft SQL Server R ServicesPredictive Analysis using Microsoft SQL Server R Services
Predictive Analysis using Microsoft SQL Server R Services
 
Analyses and processing of big data in financial services
Analyses and processing of big data in financial servicesAnalyses and processing of big data in financial services
Analyses and processing of big data in financial services
 
HTML5 features & JavaScript APIs
HTML5 features & JavaScript APIsHTML5 features & JavaScript APIs
HTML5 features & JavaScript APIs
 

Recently uploaded

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 

Recently uploaded (20)

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 

C# 7 development

  • 2. 2 Presenter C# 7 Development Fisnik Doko • Microsoft Certified Trainer • MCSD, MCPD, MCSE, MCSA, MCTS, MCP, MCT • Senior Software Developer / Architect – NBRM • Speaker • Consultant
  • 3. Agenda C# Development C# and versions .01 .02 C# 7.0, 7.1, 7.2, 7.3 .03 C# 8.0
  • 6. 6 C# 7 – How do I get it • Visual Studio 2017 • Nuget package Microsoft.Net.Compilers • Roslyn compiler from GitHub • Online https://sharplab.io/
  • 7. 7 Configure Build for language version
  • 8. 8 New Features in C# 7.0, 7.1, 7.2, 7.3 C# 7.0 C# 7.1 C# 7.2 C# 7.3 • Binary Literals • Digit Separators • Local Functions • Out Variables Tuples • Deconstruction • Discards • Ref Returns & Locals • Pattern Matching • Expression Bodied Members • Throw Expression • Async main • Default literal expressions • Inferred tuple element names • Reference assembly generation • Digital Separator after Base Specifier • Non-trailing named arguments • Leading underscores in numeric literals • private protected access modifier • Reference semantics with value types • Ref Local Reassignment • Overload resolution • Attributes on backing fields • Tuple comparison • Expression variables in initializers VS 2017 March 2017 VS 2017 V15.3 August 2017 VS 2017 v15.5 January 2018 VS 2017 v15.7 May 2018
  • 9. 9 Defining out Parameters Inline • C# 7 allows you to declare out parameters inline, during a method call • Cleaner syntax - no need to pre-declare before the method call • Also ensures you can't use the variable before the method call So the following examples are equivalent: C# 7 int prod, quot; MethodOut(num1, num2, out prod, out quot); Console.WriteLine("After MethodOut, prod={0}, quot={1}", prod, quot); MethodOut(num1, num2, out int p, out int q); Console.WriteLine("After MethodOut, using C# 7 syntax, p={0}, q={1}", p, q);
  • 10. 10 Numeric Literals • Binary literals • Allows you to use binary for integer literals (use the prefix 0b) • Useful if you're doing a lot of bit manipulation • Digit separators • Allows you to embed _ anywhere in numeric literals (integer, long, decimal, double) • Makes it easier to read big numbers C# 7 int six = 0b110; int thirtyFive = 0b100011; int sixtyFive = 0b1000001; long idealAnnualSalary = 123_456____789; double electronicCharge = 1.602_176_62E-19;
  • 11. 11 Expression-Body Members in C# 6 • C# 6 introduced the idea of "expression-bodied members“ • Members that comprises a simple expression (via lambda syntax) • C# 6 supports the following expression-bodied members: • Methods • Read-only properties C# 6 class Employee { public string Name { get; set; } public double Salary { get; set; } public void PayRaise(double amount) => Salary += amount; // Method public double TaxPayable => Salary * 0.25; // Read-only property …
  • 12. 12 Expression-Body Members in C# 7 • C# 7 also allows expression-bodied members for: • Constructors • Finalizers (i.e. destructors) • Property getters and setters C# 7 class Contact { private string name; public Contact(string name) => this.name = name; // Constructor ~Contact() => Console.WriteLine($"Finalized {name}"); // Finalizer public string Name { get => name; // Property getter set => name = value ?? "Anonymous"; // Property setter }
  • 13. 13 Exception Filters • C# 6 and above allows you to define exception filters • Append a when boolean expression to a catch clause • If the expression is true, the catch block is run (otherwise the exception keeps going) C# 6 try { … } catch (SomeException ex) when (MyFilterFuncThatReturnsBoolean(ex)) { … }
  • 14. 14 C# 7 Throw Expressions • Prior to C# 7, you couldn't throw an exception in the middle of another expression • So the following statements won't compile prior to C# 7 • C# 7 now supports "throw expressions", so you use throw an exception in the middle of an another expression C# 7 string variable1 = SomeFuncToCalcValue() ?? throw new SomeException("Error!"); string variable2 = (someTest) ? "hooray!" : throw new SomeException("Error!"); String Name { get => name; set => name = value ?? throw new SomeException("Error!"); }
  • 15. 15 Tuples • Tuples are a new feature in C# 7 • A tuple is like an anonymous bag of related data fields • The data fields can be of different types • Use of tuples: • Handy if you want to group some data fields together, without the overhead of defining a separate class/struct • E.g. returning multiple values from a function • Note: • Prior to .NET 4.7, you must install the System.ValueTuple NuGet package in your project if you want to use tuples C# 7
  • 16. 16 Tuples Syntax • You can assign a tuple to a var variable • By default, the tuple fields are named Item1, Item2, etc • You can specify field names when you create a tuple • Alternatively you can specify field names by assigning the tuple to named variables enclosed in () C# 7 var personA = ("Matthew", 21); Console.WriteLine($"{personA.Item1} is {personA.Item2} years old"); var personB = (Name: "Mark", Age: 22); Console.WriteLine($"{personB.Name} is {personB.Age} years old"); (string name, int age) = ("Luke", 23); Console.WriteLine($"{name} is {age} years old");
  • 17. 17 Using Tuples in Functions • A function can return multiple values via a tuple • Client code can call the function and use the tuple as follows: • Alternatively, client code can unpick the tuple fields like this: C# 7 private static (double avg, int count) GetStats(params int[] nums) { .. return (sum/i, i); } var stats = GetStats(10, 20, 30, 40); Console.WriteLine($"Average is {stats.avg}, count of items is {stats.count}"); (double a, int n) = GetStats(50, 60, 70, 80); Console.WriteLine($"Average is {a}, count of items is {n}");
  • 18. 18 Deconstruct • You can define a Deconstruct() method in classes and structs, to extract multiple values from an object • Client code can assign an object directly to a tuple C# 7 public class Product { … public void Deconstruct(out string description, out double unitPrice) { description = this.Description; unitPrice = this.UnitPrice; } } Product productA = new Product("The Good Book", 12.99); (string desc, double price) = productA; Console.WriteLine($"{desc} costs {price:c}");
  • 19. 19 Discards • Discards are temporary, write-only variables used in assignments when you don't care about the value assigned • Discards can reduce memory allocations • Variable is a discard by assigning it the underscore (_) as its name In C# 7.0, discards are supported in assignments in the following contexts: • Tuple and object deconstruction • Calls to methods with out parameters C# 7 (_, _, area) = city.GetCityInformation(cityName); Result(a, b, out int sum, out int _, out int _, out int _, out int _); if (int.TryParse(ReadLine(), out int _))
  • 20. 20 Local Functions • C# 7 allows you to define functions inside other functions • Local functions may use variables from the enclosing scope • Have all features of a regular method except that local functions cannot be static in nature • Scenario: • You're implementing function A, and it's starting to get messy • You can move some complexity into another function B • You can put function B inside function A, if it's only needed there • Motivation: • Reduce the complexity of the class as a whole, also reducing memory allocation C# 7
  • 21. 21 Defining ref Return Values • C# 7 allows a function to return a reference • Decorate the function return type with ref • Also decorate all return values with ref C# 7 static ref double FindMinElem(double[] arr) { int minpos = 0; for (int i = 0; i < arr.Length; i++) { if (arr[i] < arr[minpos]) minpos = i; } return ref arr[minpos]; }
  • 22. 22 Defining ref Local Variables • When you call a function that returns a reference… • If you assign the result to a variable, it just holds a copy • If you assign the result to a ref variable, it holds a reference C# 7 double [] array = { 10.5, 3.14, 99.9, 64.1 }; double minElem = FindMinElem(array); // minElem holds a copy of the element value. Console.WriteLine($"nValue of min element: {minElem}"); minElem = 1.2345; // Modifies local variable, not the array element. Console.WriteLine($"Value of element [1]: {array[1]}"); double [] array = { 10.5, 3.14, 99.9, 64.1 }; ref double refMinElem = ref FindMinElem(array); // refMinElem refers to element. Console.WriteLine($"nValue of min element: {refMinElem}"); refMinElem = 1.2345; // Modifies the actual array element now. Console.WriteLine($"Value of element [1]: {array[1]}");
  • 23. 23 Pattern Matching via "is" Expressions • In C# 7, you can use is expressions for pattern matching in if statements • You can test if a value equals some constant • You can also test if a value is some type (if so, it is automatically cast and assigned to a fresh variable of that type) C# 7 static private int ParseValue(object value) { if (value is null) return 0; else if (value is int iValue) return iValue; else if (value is double dValue) return (int)Math.Round(dValue); else if (value is string strValue) return int.Parse(strValue); else return 0; }
  • 24. 24 Pattern Matching in switch Statements • C# 7 also allows the following in a switch statement: • In a case branch, specify the type you want to test against • You can use a when clause to refine a case test • You can mix-and-match constants and types in case branches C# 7 static private int ParseValue(object value) { if (value is null) return 0; else if (value is int iValue) return iValue; else if (value is double dValue) return (int)Math.Round(dValue); else if (value is string strValue) return int.Parse(strValue); else return 0; }
  • 25. 25 Generalized async return types • In C# 7, there is an inbuilt value type ValueTask <T> which can be used instead of Task<T> • NuGet package System.Threading.Tasks.Extensions • Not replace Task, helps to reduce the number of allocation if is invoked frequently C# 7 static void Main(string[] args) { int x= GetLargestPrimeNumber(20).Result; . } private static async ValueTask<int> GetLargestPrimeNumber(int maxNumber) { ...
  • 26. 26 Async Main Methods • Allow await to be used in an application's Main method by allowing the entry point to return Task / Task<int> and be marked async Extends the list of allowed entry points to include: • static async Task Main() • static Task Main() • static async Task<int> Main() • static Task<int> Main() • static async Task Main(string[]) • static Task Main(string[]) • static async Task<int> Main(string[]) • static Task<int> Main(string[]) C# 7.1
  • 28. 28 The default Literal • The target-typed default feature is a shorter form variation of the default(T) operator, which allows the type to be omitted • Its type is inferred by target-typing instead C# 7.1 // from C# 7.1 int a = default; bool b = default; string c = default; int? d = default; Action<int, bool> action = default; Predicate<string> predicate = default; List<string> list = default; int a = default(int); bool b = default(bool); string c = default(string); int? d = default(int?); Action<int, bool> action = default(Action<int, bool>); Predicate<string> predicate = default(Predicate<string>); List<string> list = default(List<string>);
  • 29. 29 Inferred Tuple Element Names • Infer tuple names from parameters • Similar to anonymous types C# 7.1
  • 30. 30 C# 7.2 • Digital Separator after Base Specifier • int binaryValue = 0b_0101_0101; • Non-trailing Named Arguments • Method calls may now use named arguments that precede positional arguments when those named arguments are in the correct positions C# 7.2
  • 31. 31 C# 7.2 • Private Protected access modifier • members will only be visible to subclasses in the same assembly • Ref Conditional Expression C# 7.2 ref var firstItem = ref (emptyArray.Length > 0 ? ref emptyArray[0] : ref nonEmptyArray[0]);
  • 32. 32 C# 7.3 • Ref Local Reassignment • Now, ref locals may be reassigned to refer to different instances after being initialized • Additional Generic Constraints • You can now specify the type System.Enum or System.Delegate as base class constraints for a type parameter C# 7.3 ref var max = ref b; if (a > b) { max = ref a; } public static TDelegate Combine<TDelegate>(...) where TDelegate : Delegate public static string GetDescription<TEnum>(...) where TEnum : Enum
  • 33. 33 C# 7.3 • Attributes Targeting Fields of Auto-Implemented Properties • Expression Variables in Initializers • out variable declarations has been extended to include field initializers, property initializers, constructor initializers • Equality Operators for Value Tuples C# 7.3 [field: NonSerialized] public double X { get; set; }
  • 34. 34 C# 8.0 planned features • Asynchronous Streams / Sequences • Asynchronous Dispose • Extension everything • Adding support for properties, static methods • Records • Lightweight class only with fields C# 8 public class Sword(int Damage, int Durability);
  • 35. 35 C# 8.0 planned features • Ranges • new syntax for expressing a range of values • Nullable reference types • Default interface implementations C# 8 var range = 1..5; … foreach (var index in min..max) { } String? s = null;