C# 6.0 - What?! C# is being updated?

58
C# 6.0 What?! C# is Being Updated? Filip Ekberg

Transcript of C# 6.0 - What?! C# is being updated?

Page 1: C# 6.0 - What?! C# is being updated?

C# 6.0What?! C# is Being Updated?

Filip Ekberg

Page 2: C# 6.0 - What?! C# is being updated?

Page / Copyright ©2014 by Readify Pty Ltd2 @fekberg

Page 3: C# 6.0 - What?! C# is being updated?

Page

Filip EkbergC# MVP

http://fekberg.com

@fekberg

C# Smorgasbord

/ Copyright ©2014 by Readify Pty Ltd3

Page 4: C# 6.0 - What?! C# is being updated?

Page

History of C#

/ Copyright ©2014 by Readify Pty Ltd4

http://en.wikipedia.org/wiki/C_Sharp_(programming_language)#History

Page 5: C# 6.0 - What?! C# is being updated?

Page

C# 2.0› Generics

› Partial types

› Anonymous methods

› Iterators

› Nullable types

› Getter/Setter separate accessibility

› Static classes

› And more..

/ Copyright ©2014 by Readify Pty Ltd5 @fekberg

Page 6: C# 6.0 - What?! C# is being updated?

Page

C# 3.0› Implicitly typed local variables

› Object and collection initializers

› Auto-properties

› Anonymous types

› Extension methods

› Query expressions

› Lambda expressions

› Expression trees

› And more..

/ Copyright ©2014 by Readify Pty Ltd6 @fekberg

Page 7: C# 6.0 - What?! C# is being updated?

Page

C# 4.0› Dynamic binding

› Named and optional parameters

› Generic co-and contravariance

› And more..

/ Copyright ©2014 by Readify Pty Ltd7 @fekberg

Page 8: C# 6.0 - What?! C# is being updated?

Page

C# 5.0› Asynchronous methods

› Caller info attributes

/ Copyright ©2014 by Readify Pty Ltd8 @fekberg

Page 9: C# 6.0 - What?! C# is being updated?

Page

The state of the Compiler› It’s been a black box

› Roslyn to the rescue!

› .NET Compiler Platform

› Microsoft decides to re-write the compiler

› Compiler written in C#

› Easier to extend and maintain

› Open Source!

/ Copyright ©2014 by Readify Pty Ltd9 @fekberg

Page 10: C# 6.0 - What?! C# is being updated?

Page

C# 6.0 Overview› Auto-property initializers

› Getter-only auto-properties

› Assignment to getter-only auto-properties

from constructor

› Parameter-less structconstructors

› Using Statements for Static Members

› Dictionary Initializer

› Await in catch/finally

› Exception filters

› Expression-bodied members

/ Copyright ©2014 by Readify Pty Ltd10

› Null propagation

› String interpolation

› nameofoperator

› And more..

Page 11: C# 6.0 - What?! C# is being updated?

Page

Auto-Property Initializers

/ Copyright ©2014 by Readify Pty Ltd11

Page 12: C# 6.0 - What?! C# is being updated?

Page

Auto-property initializers in a nutshell

/ Copyright ©2014 by Readify Pty Ltd12 @fekberg

class Person{

public string Name { get; set; }}

class Person{

public string Name { get; } = "Anonymous";}

= "Anonymous";

Page 13: C# 6.0 - What?! C# is being updated?

Page

Auto-property initializers in a nutshell

/ Copyright ©2014 by Readify Pty Ltd13 @fekberg

class Person{

public string Name { get; }

public Person(){

Name = "Filip";}

}

Page 14: C# 6.0 - What?! C# is being updated?

Page

Auto-property initializers in a nutshell

/ Copyright ©2014 by Readify Pty Ltd14 @fekberg

class Person{

private readonly string <Name>k__BackingField = "Filip";

public string Name{

get{

return this.<Name>k__BackingField;}

}}

Page 15: C# 6.0 - What?! C# is being updated?

Page

Parameter-lessstructconstructors

/ Copyright ©2014 by Readify Pty Ltd15

Page 16: C# 6.0 - What?! C# is being updated?

Page

Parameter-less structconstructors in a nutshell

/ Copyright ©2014 by Readify Pty Ltd16 @fekberg

struct Point{

public int X { get; }public int Y { get; }

}

Read Only!

public Point(){

X = 100;Y = 100;

}

Page 17: C# 6.0 - What?! C# is being updated?

Page

Parameter-less structconstructors in a nutshell

/ Copyright ©2014 by Readify Pty Ltd17 @fekberg

Read Only!

struct Point{

private readonly int <X>k__BackingField;private readonly int <Y>k__BackingField;

public int X{

get{

return this.<X>k__BackingField;}

}public int Y{

get{

return this.<Y>k__BackingField;}

}public Point(){

this.<X>k__BackingField = 100;this.<Y>k__BackingField = 100;

}}

Page 18: C# 6.0 - What?! C# is being updated?

Page

Parameter-less structconstructors in a nutshell

/ Copyright ©2014 by Readify Pty Ltd18 @fekberg

Read Only!

struct Point{

public int X { get; }public int Y { get; }

public Point(int x, int y){

X = x;Y = y;

}

public Point() : this(100, 100){}

}

Page 19: C# 6.0 - What?! C# is being updated?

Page

Using Statements for Static Members

/ Copyright ©2014 by Readify Pty Ltd19

Page 20: C# 6.0 - What?! C# is being updated?

Page

Using Statements for Static Members in a nutshell

/ Copyright ©2014 by Readify Pty Ltd20 @fekberg

class Program{

static void Main(string[] args){

var angle = 90d;Console.WriteLine(Math.Sin(angle));

}}

Page 21: C# 6.0 - What?! C# is being updated?

Page

Using Statements for Static Members in a nutshell

/ Copyright ©2014 by Readify Pty Ltd21 @fekberg

using System.Console;using System.Math;

class Program{

static void Main(string[] args){

var angle = 90d;WriteLine(Sin(angle));

}}

Page 22: C# 6.0 - What?! C# is being updated?

Page

Using Statements for Static Members in a nutshell

/ Copyright ©2014 by Readify Pty Ltd22 @fekberg

using System.Console;using System.Linq.Enumerable;

class Program{

static void Main(string[] args){

foreach (var i in Range(0, 10)){

WriteLine(i);}

}}

Page 23: C# 6.0 - What?! C# is being updated?

Page

Dictionary Initializers

/ Copyright ©2014 by Readify Pty Ltd23

Page 24: C# 6.0 - What?! C# is being updated?

Page

Dictionary Initializers in a nutshell

/ Copyright ©2014 by Readify Pty Ltd24 @fekberg

var people = new Dictionary<string, Person>{

["Filip"] = new Person()};

var answers = new Dictionary<int, string>{

[42] = "the answer to life the universe and everything"};

Page 25: C# 6.0 - What?! C# is being updated?

Page

Await inside Finally block

/ Copyright ©2014 by Readify Pty Ltd25

Page 26: C# 6.0 - What?! C# is being updated?

Page

Await + Finally in a nutshell

/ Copyright ©2014 by Readify Pty Ltd26 @fekberg

public async Task DownloadAsync(){

}

try{ }catch{

await Task.Delay(2000);}finally{

await Task.Delay(2000);}

Page 27: C# 6.0 - What?! C# is being updated?

Page

Exception Filters

/ Copyright ©2014 by Readify Pty Ltd27

Page 28: C# 6.0 - What?! C# is being updated?

Page

Exception filters in a nutshell

/ Copyright ©2014 by Readify Pty Ltd28 @fekberg

try{

throw new CustomException { Severity = 100 };}catch (CustomException ex) if (ex.Severity > 50){

Console.WriteLine("*BING BING* WARNING *BING BING*");}catch (CustomException ex){

Console.WriteLine("Whooops!");}

Page 29: C# 6.0 - What?! C# is being updated?

Page

Null Propagation

/ Copyright ©2014 by Readify Pty Ltd29

Page 30: C# 6.0 - What?! C# is being updated?

Page

Null propagation in a nutshell

/ Copyright ©2014 by Readify Pty Ltd30 @fekberg

class Person{

public string Name { get; set; }public Address Address { get; set; }

}

Console.WriteLine(filip.Address.AddressLine1);

var filip = new Person{

Name = "Filip"};

class Address{

public string AddressLine1 { get; set; }public string AddressLine2 { get; set; }

}

Page 31: C# 6.0 - What?! C# is being updated?

Page

Null propagation in a nutshell

/ Copyright ©2014 by Readify Pty Ltd31 @fekberg

class Person{

public string Name { get; set; }public Address Address { get; set; }

}

Console.WriteLine(filip.Address.AddressLine1);

var filip = new Person{

Name = "Filip"};

class Address{

public string AddressLine1 { get; set; }public string AddressLine2 { get; set; }

}

Page 32: C# 6.0 - What?! C# is being updated?

Page

Null propagation in a nutshell

/ Copyright ©2014 by Readify Pty Ltd32 @fekberg

class Person{

public string Name { get; set; }public Address Address { get; set; }

}

Console.WriteLine(filip.Address.AddressLine1);Console.WriteLine(filip.Address == null ? "No Address" : filip.Address.AddressLine1);

var filip = new Person{

Name = "Filip"};

class Address{

public string AddressLine1 { get; set; }public string AddressLine2 { get; set; }

}

Page 33: C# 6.0 - What?! C# is being updated?

Page

Null propagation in a nutshell

/ Copyright ©2014 by Readify Pty Ltd33 @fekberg

class Person{

public string Name { get; set; }public Address Address { get; set; }

}

Console.WriteLine(filip.Address.AddressLine1);Console.WriteLine(filip.Address == null ? "No Address" : filip.Address.AddressLine1);

Console.WriteLine(filip?.Address?.AddressLine1 ?? "No Address");

var filip = new Person{

Name = "Filip"};

class Address{

public string AddressLine1 { get; set; }public string AddressLine2 { get; set; }

}

Page 34: C# 6.0 - What?! C# is being updated?

Page

Null propagation in a nutshell

/ Copyright ©2014 by Readify Pty Ltd34 @fekberg

var people = new[]{

new Person(),null

};

WriteLine(people[0]?.Name);

WriteLine(people[1]?.Name);

Page 35: C# 6.0 - What?! C# is being updated?

Page

Null propagation in a nutshell

/ Copyright ©2014 by Readify Pty Ltd35 @fekberg

Person[] people = null;

WriteLine(people?[0]?.Name);

Person[] people = null;Console.WriteLine(

(people != null) ? ((people[0] == null) ? null : people[0].Name) : null

);

Page 36: C# 6.0 - What?! C# is being updated?

Page

Expression-bodied members

/ Copyright ©2014 by Readify Pty Ltd36

Page 37: C# 6.0 - What?! C# is being updated?

Page

Expression-bodied members in a nutshell

/ Copyright ©2014 by Readify Pty Ltd37 @fekberg

class Rectangle{

public double Width { get; set; }public double Height { get; set; }

public double Area => Width * Height;}

Page 38: C# 6.0 - What?! C# is being updated?

Page

Expression-bodied members in a nutshell

/ Copyright ©2014 by Readify Pty Ltd38 @fekberg

class Rectangle{

public double Width { get; set; }public double Height { get; set; }

public override string ToString() => "My Width is \{Width} and my Height is \{Height}";

}

Page 39: C# 6.0 - What?! C# is being updated?

Page

String interpolation

/ Copyright ©2014 by Readify Pty Ltd39

Page 40: C# 6.0 - What?! C# is being updated?

Page

String interpolation in a nutshell

/ Copyright ©2014 by Readify Pty Ltd40 @fekberg

public override string ToString() => "My Width is \{Width} and my Height is \{Height}";

public override string ToString() => $"My Width is {Width} and my Height is {Height}";

Syntax will change in a later release to the following:

Page 41: C# 6.0 - What?! C# is being updated?

Page

String interpolation in a nutshell

/ Copyright ©2014 by Readify Pty Ltd41 @fekberg

public override string ToString() => "My Width is \{Width} and my Height is \{Height}";

public override string ToString(){

object[] args = new object[] { this.Width, this.Height };return string.Format("My Width is {0} and my Height is {1}", args);

}

Page 42: C# 6.0 - What?! C# is being updated?

Page

String interpolation in a nutshell

/ Copyright ©2014 by Readify Pty Ltd42 @fekberg

int age = 28;var result = "Hello there, I'm \{age : D5} years old!";WriteLine(result);

int num = 28;object[] objArray1 = new object[] { num };Console.WriteLine(string.Format("Hello there, I'm {0:D5} years old!", objArray1));

Page 43: C# 6.0 - What?! C# is being updated?

Page

nameofoperator

/ Copyright ©2014 by Readify Pty Ltd43

Page 44: C# 6.0 - What?! C# is being updated?

Page

nameof operator in a nutshell

/ Copyright ©2014 by Readify Pty Ltd44 @fekberg

static void Main(string[] args){

WriteLine("Parameter name is: \{nameof(args)}");}

Page 45: C# 6.0 - What?! C# is being updated?

Page

nameof operator in a nutshell

/ Copyright ©2014 by Readify Pty Ltd45 @fekberg

public double CalculateArea(int width, int height){

if (width <= 0){

throw new ArgumentException("Parameter \{nameof(width)} cannot be less than 0");}

return width * height;}

Page 46: C# 6.0 - What?! C# is being updated?

Page

DemoC# 6.0 Language Features in Visual Studio 2015

/ Copyright ©2014 by Readify Pty Ltd46

Page 47: C# 6.0 - What?! C# is being updated?

Page

There’s more??

/ Copyright ©2014 by Readify Pty Ltd47

Page 48: C# 6.0 - What?! C# is being updated?

Page

What about C# 7.0?› Binary literals and Digit separators

/ Copyright ©2014 by Readify Pty Ltd48

0b00001000

0xFF_00_FA_AF

Page 49: C# 6.0 - What?! C# is being updated?

Page

What about C# 7.0?› Binary literals and Digit separators

› Event initializers

/ Copyright ©2014 by Readify Pty Ltd49

var client = new WebClient{

DownloadFileCompleted += DownloadFileCompletedHandler

};

Page 50: C# 6.0 - What?! C# is being updated?

Page

What about C# 7.0?› Binary literals and Digit separators

› Event initializers

› Field targets on auto-properties

/ Copyright ©2014 by Readify Pty Ltd50

[field: NonSerialized]public int Age { get; set; }

Page 51: C# 6.0 - What?! C# is being updated?

Page

What about C# 7.0?› Binary literals and Digit separators

› Event initializers

› Field targets on auto-properties

› Semicolon operator

/ Copyright ©2014 by Readify Pty Ltd51

var y = (var x = Foo(); Write(x); x * x);

Page 52: C# 6.0 - What?! C# is being updated?

Page

What about C# 7.0?› Binary literals and Digit separators

› Event initializers

› Field targets on auto-properties

› Semicolon operator

› Using params with IEnumerable

/ Copyright ©2014 by Readify Pty Ltd52

int Avg(params IEnumerable<int> numbers)

Page 53: C# 6.0 - What?! C# is being updated?

Page

What about C# 7.0?› Binary literals and Digit separators

› Event initializers

› Field targets on auto-properties

› Semicolon operator

› Using params with IEnumerable

› Declaration Expressions

/ Copyright ©2014 by Readify Pty Ltd53

public void CalculateAgeBasedOn(int birthYear, out int age){

age = DateTime.Now.Year - birthYear;}

CalculateAgeBasedOn(1987, out var age);

Page 54: C# 6.0 - What?! C# is being updated?

Page

What about C# 7.0?› Binary literals and Digit separators

› Event initializers

› Field targets on auto-properties

› Semicolon operator

› Using params with IEnumerable

› Declaration Expressions

› Primary Constructors

› And possibly more...

/ Copyright ©2014 by Readify Pty Ltd54

class Person(string name, int age){

private string _name = name;private int _age = age;

}

Page 55: C# 6.0 - What?! C# is being updated?

Page

More with Roslyn

/ Copyright ©2014 by Readify Pty Ltd55

› Exposes the Compiler APIs

› Plugins powered by Roslyn

› Analyze code

› Re-write code

@fekberg

Page 56: C# 6.0 - What?! C# is being updated?

Page

Summary

/ Copyright ©2014 by Readify Pty Ltd56

› C# 6.0 is awesome

› There’s a lot of change in C# 6.0

› .NET Compiler Platform ("Roslyn") makes it easy for Microsoft to

improve the language

› There’s a lot of interesting features that could go in C# 7.0

@fekberg

Page 57: C# 6.0 - What?! C# is being updated?

Page

Questions?

/ Copyright ©2014 by Readify Pty Ltd57

Page 58: C# 6.0 - What?! C# is being updated?

Page

Thank you!

http://fekberg.com

@fekberg

C# Smorgasbord

/ Copyright ©2014 by Readify Pty Ltd58