C# 6download.microsoft.com/download/C/5/7/C57FB17E-620C-46AD-BC3… · •String interpolation...

Post on 22-May-2020

11 views 0 download

Transcript of C# 6download.microsoft.com/download/C/5/7/C57FB17E-620C-46AD-BC3… · •String interpolation...

C# 6Door Alex en Chris van Beek

Over ons

• Enthousiastelingen• Software Architecten bij Luminis Arnhem B.V.• Gespecialiseerd in Microsoft technologie: .Net, Azure en

Windows• Twitter: @Beekje en @cbeek• Blog: http://arnhem.luminis.eu/alex-van-beek/ en

http://arnhem.luminis.eu/chris-van-beek

Inleiding

Elke C# release heeft een thema:

• C# 2: Generics / Collecties

• C# 3: LINQ

• C# 4: Interop

• C# 5: Asynchroniteit

• C# 6: Verminderen “boiler plate” code

Features

• String interpolation

• Automatic Property Initializers

• Indexed Member Initialization

• Static Using Statements

• NULL-Conditional operator

• NameOf expressies

• Exception filters

• Await in Catch / Finally blokken

• Expression-bodied members

Overzicht

• String interpolation

• Automatic Property Initializers

• Indexed Member Initialization

• Static Using Statements

• NULL-Conditional operator

• NameOf expressies

• Exception filters

• Await in Catch / Finally blokken

• Expression-bodied members

String Interpolation

Vervanging voor o.a. String.format

Console.WriteLine($"{p.X},{p.Y}");

Console.WriteLine(String.Format("{0},{1}", p.X,p.Y));

Demo: String interpolation

String Interpolation weetjes

• Kan gebruik maken van de IFormattable interface

• “@” is in combinatie met “$” te gebruiken

Overzicht

• String interpolation

• Automatic Property Initializers

• Indexed Member Initialization

• Static Using Statements

• NULL-Conditional operator

• NameOf expressies

• Exception filters

• Await in Catch / Finally blokken

• Expression-bodied members

Automatic Property Initializers

Handige manier om properties te initialiseren.

• Werkt ook voor read-only properties

public string Name { get; private set; } = "Unknown";

public Person(){

Name = "Unknown";}

Demo: Automatic Property

Initializers

Automatic Property Initializers

weetjes

• Je hebt geen setter meer nodig voor het toekennen

aan read-only properties vanuit constructoren

Overzicht

• String interpolation

• Automatic Property Initializers

• Indexed Member Initialization

• Static Using Statements

• NULL-Conditional operator

• NameOf expressies

• Exception filters

• Await in Catch / Finally blokken

• Expression-bodied members

Indexed Member Initialization

Net een iets handigere manier om indexers te

initializeren.

Dictionary<string, string> translations = new Dictionary<string, string> {

["Koe"] = "Cow" ,["Kat"] = "Cat",["Hond"]= "Dog"

};

Dictionary<string, string> translations = new Dictionary<string, string>();

translations["Koe"] = "Cow";translations["Kat"] = "Cat";translations["Hond"] = "Dog";

Demo: Indexed Member

Initialization

Indexed Member Initialization

weetjes• Werkt ook voor types zonder IEnumerable en zonder

Add methode

Overzicht

• String interpolation

• Automatic Property Initializers

• Indexed Member Initialization

• Static Using Statements

• NULL-Conditional operator

• NameOf expressies

• Exception filters

• Await in Catch / Finally blokken

• Expression-bodied members

Static using statements

Vermindert typwerk bij het veelvuldig aanroepen van

static methoden

WriteLine("Hello, my name is Alex van Beek... Who are you?: ");

System.Console.WriteLine("Hello, my name is Alex van Beek... Who are you?: ");

Demo: Static Using

Statements

Static using weetjes

• Kan gebruikt worden om extensie methoden

preciezer te scopen dan de hele namespace

Overzicht

• String interpolation

• Automatic Property Initializers

• Indexed Member Initialization

• Static Using Statements

• NULL-Conditional operator

• NameOf expressies

• Exception filters

• Await in Catch / Finally blokken

• Expression-bodied members

NULL-Conditional operator

Vermindert het typwerk bij geneste NULL controles

value = value?.Substring(0,3);

if(value != null){

value = value.substring(0, 3);}

Demo: NULL Conditional

Operator

NULL-Conditional Operator weetjes

• Zet valuetypes om naar nullable types

• Ook te gebruiken bij indexers

Overzicht

• String interpolation

• Automatic Property Initializers

• Indexed Member Initialization

• Static Using Statements

• NULL-Conditional operator

• NameOf expressies

• Exception filters

• Await in Catch / Finally blokken

• Expression-bodied members

NameOf expressies

Vermindert kans op fouten bij het verwijzen naar

members

Guard.GuardNull(argument, nameof(argument));

Guard.GuardNull(argument, "argument");

Demo: nameof operator

nameof weetjes

• Erg handig bij WPF en INotifyPropertyChanged

Overzicht

• String interpolation

• Automatic Property Initializers

• Indexed Member Initialization

• Static Using Statements

• NULL-Conditional operator

• NameOf expressies

• Exception filters

• Await in Catch / Finally blokken

• Expression-bodied members

Exception filters

Geeft ons de mogelijkheid om fijnmaziger aan te geven

welke excepties wij willen afvangen

try{

throw new Exception("E1");}catch (Exception ex) when (ex.Message == "E1"){

Console.WriteLine("caught E1");}catch (Exception ex) when (ex.Message == "E2"){

Console.WriteLine("caught E2");}

Demo: Exception filters

Exception filters weetjes

• Was al aanwezig in IL

• Is anders dan een conditie in een catch blok en

daarna een rethrow.

Overzicht

• String interpolation

• Automatic Property Initializers

• Indexed Member Initialization

• Static Using Statements

• NULL-Conditional operator

• NameOf expressies

• Exception filters

• Await in Catch / Finally blokken

• Expression-bodied members

Await en catch / finally

Werkt eindelijk zoals verwacht

try{

Error.Cause();}catch{

await Log.InfoAsync("Error logged");}

Overzicht

• String interpolation

• Automatic Property Initializers

• Indexed Member Initialization

• Static Using Statements

• NULL-Conditional operator

• NameOf expressies

• Exception filters

• Await in Catch / Finally blokken

• Expression-bodied members

Expression-bodied members

“Syntactic sugar” om eenregelige methodes iets korter

op te schrijven

public static void WriteSomething(string message) => Console.WriteLine(message);

public static void WriteSomething(string message){ Console.WriteLine(message); }

Expression-bodied members

weetjes• Met name handig om berekende read-only properties

te maken

Overzicht

• String interpolation

• Automatic Property Initializers

• Indexed Member Initialization

• Static Using Statements

• NULL-Conditional operator

• NameOf expressies

• Exception filters

• Await in Catch / Finally blokken

• Expression-bodied members

Features die zijn vervallen

of nog niet af zijn• Vervallen:

• Primary Constructors

• Declaratie expressies

• Nog niet af maar wel in C#6 (waarschijnlijk):

• Binaire literals: int i = 0b01110011;

• Underscore als groepskarakter: long l =

1_000_000_000;

Vragen?

Your feedback is important!

Scan the QR Code and let us know via the TechDays App.

Laat ons weten wat u van de sessie vindt via de TechDays App!

Scan de QR Code.

Bent u al lid van de Microsoft Virtual Academy?! Op MVA kunt u altijd iets nieuws leren over de laatste technologie van Microsoft. Meld u vandaag aan op de MVA Stand. MVA biedt 7/24 gratis online training on-demand voor IT-Professionals en Ontwikkelaars.