Upload
filip-ekberg
View
6.619
Download
2
Embed Size (px)
Citation preview
C# 6.0What?! C# is Being Updated?
Filip Ekberg
Page / Copyright ©2014 by Readify Pty Ltd2 @fekberg
Page
Filip EkbergC# MVP
http://fekberg.com
@fekberg
C# Smorgasbord
/ Copyright ©2014 by Readify Pty Ltd3
Page
History of C#
/ Copyright ©2014 by Readify Pty Ltd4
http://en.wikipedia.org/wiki/C_Sharp_(programming_language)#History
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
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
C# 4.0› Dynamic binding
› Named and optional parameters
› Generic co-and contravariance
› And more..
/ Copyright ©2014 by Readify Pty Ltd7 @fekberg
Page
C# 5.0› Asynchronous methods
› Caller info attributes
/ Copyright ©2014 by Readify Pty Ltd8 @fekberg
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
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
Auto-Property Initializers
/ Copyright ©2014 by Readify Pty Ltd11
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
Auto-property initializers in a nutshell
/ Copyright ©2014 by Readify Pty Ltd13 @fekberg
class Person{
public string Name { get; }
public Person(){
Name = "Filip";}
}
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
Parameter-lessstructconstructors
/ Copyright ©2014 by Readify Pty Ltd15
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
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
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
Using Statements for Static Members
/ Copyright ©2014 by Readify Pty Ltd19
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
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
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
Dictionary Initializers
/ Copyright ©2014 by Readify Pty Ltd23
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
Await inside Finally block
/ Copyright ©2014 by Readify Pty Ltd25
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
Exception Filters
/ Copyright ©2014 by Readify Pty Ltd27
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
Null Propagation
/ Copyright ©2014 by Readify Pty Ltd29
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
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
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
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
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
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
Expression-bodied members
/ Copyright ©2014 by Readify Pty Ltd36
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
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
String interpolation
/ Copyright ©2014 by Readify Pty Ltd39
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
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
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
nameofoperator
/ Copyright ©2014 by Readify Pty Ltd43
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
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
DemoC# 6.0 Language Features in Visual Studio 2015
/ Copyright ©2014 by Readify Pty Ltd46
Page
There’s more??
/ Copyright ©2014 by Readify Pty Ltd47
Page
What about C# 7.0?› Binary literals and Digit separators
/ Copyright ©2014 by Readify Pty Ltd48
0b00001000
0xFF_00_FA_AF
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
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
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
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
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
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
More with Roslyn
/ Copyright ©2014 by Readify Pty Ltd55
› Exposes the Compiler APIs
› Plugins powered by Roslyn
› Analyze code
› Re-write code
@fekberg
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
Questions?
/ Copyright ©2014 by Readify Pty Ltd57
Page
Thank you!
http://fekberg.com
@fekberg
C# Smorgasbord
/ Copyright ©2014 by Readify Pty Ltd58