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

Filip Ekberg
Filip EkbergSenior Software Engineer at Invoice2go
C# 6.0 
What?! C# is Being Updated? 
Filip Ekberg
Page / Copyright ©2014 2 by Readify Pty Ltd @fekberg
Page 
Filip Ekberg 
C# MVP 
http://fekberg.com 
@fekberg 
C# Smorgasbord 
/ Copyright ©2014 3 by Readify Pty Ltd
http://en.wikipedia.org/wiki/C_Sharp_(programming_language)#History 
Page 
History of C# 
/ Copyright ©2014 4 by Readify Pty Ltd
Page 
C# 2.0 
› Generics 
› Partial types 
› Anonymous methods 
› Iterators 
› Nullable types 
› Getter/Setter separate accessibility 
› Static classes 
› And more.. 
/ Copyright ©2014 5 by Readify Pty Ltd @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 6 by Readify Pty Ltd @fekberg
Page 
C# 4.0 
› Dynamic binding 
› Named and optional parameters 
› Generic co-and contravariance 
› And more.. 
/ Copyright ©2014 7 by Readify Pty Ltd @fekberg
Page 
C# 5.0 
› Asynchronous methods 
› Caller info attributes 
/ Copyright ©2014 8 by Readify Pty Ltd @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 9 by Readify Pty Ltd @fekberg
Page 
C# 6.0 Overview 
› Auto-property initializers 
› Getter-only auto-properties 
› Assignment to getter-only auto-properties 
from constructor 
› Parameter-less struct constructors 
› Using Statements for Static Members 
› Dictionary Initializer 
› Await in catch/finally 
› Exception filters 
› Expression-bodied members 
/ Copyright ©2014 10 by Readify Pty Ltd 
› Null propagation 
› String interpolation 
› nameofoperator 
› And more..
Page 
Auto-Property 
Initializers 
/ Copyright ©2014 11 by Readify Pty Ltd
Page 
Auto-property initializers in a 
nutshell 
class Person 
{ 
public string Name { get; set; } 
} 
class Person 
{ 
= "Anonymous"; 
public string Name { get; } = "Anonymous"; 
} 
/ Copyright ©2014 12 by Readify Pty Ltd @fekberg
Page 
Auto-property initializers in a 
nutshell 
class Person 
{ 
public string Name { get; } 
public Person() 
{ 
Name = "Filip"; 
} 
} 
/ Copyright ©2014 13 by Readify Pty Ltd @fekberg
Page 
Auto-property initializers in a 
nutshell 
class Person 
{ 
private readonly string <Name>k__BackingField = "Filip"; 
public string Name 
{ 
get 
{ 
return this.<Name>k__BackingField; 
} 
} 
} 
/ Copyright ©2014 14 by Readify Pty Ltd @fekberg
Page 
Parameter-less 
struct 
constructors 
/ Copyright ©2014 15 by Readify Pty Ltd
Page 
Parameter-less struct 
constructors in a nutshell 
struct Point 
{ 
public int X { get; } 
public int Y { get; } 
} 
Read Only! 
public Point() 
{ 
X = 100; 
Y = 100; 
} 
/ Copyright ©2014 16 by Readify Pty Ltd @fekberg
Page 
Parameter-less struct 
constructors in a nutshell 
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; 
} 
} 
/ Copyright ©2014 17 by Readify Pty Ltd @fekberg
Page 
Parameter-less struct 
constructors in a nutshell 
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) 
{ 
} 
} 
/ Copyright ©2014 18 by Readify Pty Ltd @fekberg
Page 
Using 
Statements for 
Static Members 
/ Copyright ©2014 19 by Readify Pty Ltd
Page 
Using Statements for Static 
Members in a nutshell 
class Program 
{ 
static void Main(string[] args) 
{ 
var angle = 90d; 
Console.WriteLine(Math.Sin(angle)); 
} 
} 
/ Copyright ©2014 20 by Readify Pty Ltd @fekberg
Page 
Using Statements for Static 
Members in a nutshell 
using System.Console; 
using System.Math; 
class Program 
{ 
static void Main(string[] args) 
{ 
var angle = 90d; 
WriteLine(Sin(angle)); 
} 
} 
/ Copyright ©2014 21 by Readify Pty Ltd @fekberg
Page 
Using Statements for Static 
Members in a nutshell 
using System.Console; 
using System.Linq.Enumerable; 
class Program 
{ 
static void Main(string[] args) 
{ 
foreach (var i in Range(0, 10)) 
{ 
WriteLine(i); 
} 
} 
} 
/ Copyright ©2014 22 by Readify Pty Ltd @fekberg
Page 
Dictionary 
Initializers 
/ Copyright ©2014 23 by Readify Pty Ltd
Page 
Dictionary Initializers in a 
nutshell 
var people = new Dictionary<string, Person> 
{ 
["Filip"] = new Person() 
}; 
var answers = new Dictionary<int, string> 
{ 
[42] = "the answer to life the universe and everything" 
}; 
/ Copyright ©2014 24 by Readify Pty Ltd @fekberg
Page 
Await inside 
Finally block 
/ Copyright ©2014 25 by Readify Pty Ltd
Page 
Await + Finally in a nutshell 
public async Task DownloadAsync() 
{ 
} 
try 
{ } 
catch 
{ 
await Task.Delay(2000); 
} 
finally 
{ 
await Task.Delay(2000); 
} 
/ Copyright ©2014 26 by Readify Pty Ltd @fekberg
Page 
Exception Filters 
/ Copyright ©2014 27 by Readify Pty Ltd
Page 
Exception filters in a nutshell 
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!"); 
} 
/ Copyright ©2014 28 by Readify Pty Ltd @fekberg
Page 
Null 
Propagation 
/ Copyright ©2014 29 by Readify Pty Ltd
Page 
Null propagation in a nutshell 
class Person 
{ 
public string Name { get; set; } 
public Address Address { get; set; } 
} 
class Address 
{ 
public string AddressLine1 { get; set; } 
public string AddressLine2 { get; set; } 
} 
var filip = new Person 
{ 
Name = "Filip" 
}; 
Console.WriteLine(filip.Address.AddressLine1); 
/ Copyright ©2014 30 by Readify Pty Ltd @fekberg
Page 
Null propagation in a nutshell 
class Person 
{ 
public string Name { get; set; } 
public Address Address { get; set; } 
} 
class Address 
{ 
public string AddressLine1 { get; set; } 
public string AddressLine2 { get; set; } 
} 
var filip = new Person 
{ 
Name = "Filip" 
}; 
Console.WriteLine(filip.Address.AddressLine1); 
/ Copyright ©2014 31 by Readify Pty Ltd @fekberg
Page 
Null propagation in a nutshell 
class Person 
{ 
public string Name { get; set; } 
public Address Address { get; set; } 
} 
class Address 
{ 
public string AddressLine1 { get; set; } 
public string AddressLine2 { get; set; } 
} 
var filip = new Person 
{ 
Name = "Filip" 
}; 
Console.WriteLine(filip.Address .=A=d dnruelslsL?i n"eN1o) ;Address" : filip.Address.AddressLine1); 
/ Copyright ©2014 32 by Readify Pty Ltd @fekberg
Page 
Null propagation in a nutshell 
class Person 
{ 
public string Name { get; set; } 
public Address Address { get; set; } 
} 
class Address 
{ 
public string AddressLine1 { get; set; } 
public string AddressLine2 { get; set; } 
} 
var filip = new Person 
{ 
Name = "Filip" 
}; 
Console.WriteLine(filip.Address .=A=d dnruelslsL?i n"eN1o) ;Address" : filip.Address.AddressLine1); 
Console.WriteLine(filip?.Address?.AddressLine1 ?? "No Address"); 
/ Copyright ©2014 33 by Readify Pty Ltd @fekberg
Page 
Null propagation in a nutshell 
var people = new[] 
{ 
new Person(), 
null 
}; 
WriteLine(people[0]?.Name); 
WriteLine(people[1]?.Name); 
/ Copyright ©2014 34 by Readify Pty Ltd @fekberg
Page 
Null propagation in a nutshell 
Person[] people = null; 
WriteLine(people?[0]?.Name); 
Person[] people = null; 
Console.WriteLine( 
(people != null) ? 
((people[0] == null) ? null : people[0].Name) 
: null 
); 
/ Copyright ©2014 35 by Readify Pty Ltd @fekberg
Page 
Expression-bodied 
members 
/ Copyright ©2014 36 by Readify Pty Ltd
Page 
Expression-bodied members in 
a nutshell 
class Rectangle 
{ 
public double Width { get; set; } 
public double Height { get; set; } 
public double Area => Width * Height; 
} 
/ Copyright ©2014 37 by Readify Pty Ltd @fekberg
Page 
Expression-bodied members in 
a nutshell 
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}"; 
} 
/ Copyright ©2014 38 by Readify Pty Ltd @fekberg
Page 
String 
interpolation 
/ Copyright ©2014 39 by Readify Pty Ltd
Page 
String interpolation in a 
nutshell 
public override string ToString() => 
"My Width is {Width} and my Height is {Height}"; 
Syntax will change in a later release to the following: 
public override string ToString() => 
$"My Width is {Width} and my Height is {Height}"; 
/ Copyright ©2014 40 by Readify Pty Ltd @fekberg
Page 
String interpolation in a 
nutshell 
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); 
} 
/ Copyright ©2014 41 by Readify Pty Ltd @fekberg
int num = 28; 
object[] objArray1 = new object[] { num }; 
Console.WriteLine(string.Format("Hello there, I'm {0:D5} years old!", objArray1)); 
Page 
String interpolation in a 
nutshell 
int age = 28; 
var result = "Hello there, I'm {age : D5} years old!"; 
WriteLine(result); 
/ Copyright ©2014 42 by Readify Pty Ltd @fekberg
Page 
nameof 
operator 
/ Copyright ©2014 43 by Readify Pty Ltd
Page 
nameof operator in a nutshell 
static void Main(string[] args) 
{ 
WriteLine("Parameter name is: {nameof(args)}"); 
} 
/ Copyright ©2014 44 by Readify Pty Ltd @fekberg
Page 
nameof operator in a nutshell 
public double CalculateArea(int width, int height) 
{ 
if (width <= 0) 
{ 
throw new ArgumentException("Parameter {nameof(width)} cannot be less than 0"); 
} 
return width * height; 
} 
/ Copyright ©2014 45 by Readify Pty Ltd @fekberg
Page 
Demo 
C# 6.0 Language Features in Visual Studio 2015 
/ Copyright ©2014 46 by Readify Pty Ltd
Page 
There’s more?? 
/ Copyright ©2014 47 by Readify Pty Ltd
Page 
What about C# 7.0? 
› Binary literals and Digit separators 
/ Copyright ©2014 48 by Readify Pty Ltd 
0b00001000 
0xFF_00_FA_AF
Page 
What about C# 7.0? 
› Binary literals and Digit separators 
› Event initializers 
/ Copyright ©2014 49 by Readify Pty Ltd 
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 50 by Readify Pty Ltd 
[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 51 by Readify Pty Ltd 
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 52 by Readify Pty Ltd 
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 53 by Readify Pty Ltd 
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 54 by Readify Pty Ltd 
class Person(string name, int age) 
{ 
private string _name = name; 
private int _age = age; 
}
Page 
More with Roslyn 
› Exposes the CompilerAPIs 
› Plugins powered by Roslyn 
› Analyze code 
› Re-write code 
/ Copyright ©2014 55 by Readify Pty Ltd 
@fekberg
Page 
Summary 
› 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 
/ Copyright ©2014 56 by Readify Pty Ltd 
@fekberg
Page 
Questions? 
/ Copyright ©2014 57 by Readify Pty Ltd
Page 
Thank you! 
http://fekberg.com 
@fekberg 
C# Smorgasbord 
/ Copyright ©2014 58 by Readify Pty Ltd
1 of 58

Recommended

C# Is The Future by
C# Is The FutureC# Is The Future
C# Is The FutureFilip Ekberg
3.7K views48 slides
Azure Mobile Services .NET Backend by
Azure Mobile Services .NET BackendAzure Mobile Services .NET Backend
Azure Mobile Services .NET BackendFilip Ekberg
2.9K views36 slides
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes by
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes Michael Kimathi
257 views91 slides
20130530-PEGjs by
20130530-PEGjs20130530-PEGjs
20130530-PEGjszuqqhi 2
2.6K views37 slides
Formatting ForThe Masses by
Formatting ForThe MassesFormatting ForThe Masses
Formatting ForThe MassesHolger Schill
477 views58 slides
What's new in c# 10 by
What's new in c# 10What's new in c# 10
What's new in c# 10Moaid Hathot
202 views100 slides

More Related Content

What's hot

Deep dive into Xtext scoping local and global scopes explained by
Deep dive into Xtext scoping local and global scopes explainedDeep dive into Xtext scoping local and global scopes explained
Deep dive into Xtext scoping local and global scopes explainedHolger Schill
3.4K views74 slides
groovy DSLs from beginner to expert by
groovy DSLs from beginner to expertgroovy DSLs from beginner to expert
groovy DSLs from beginner to expertPaul King
9.8K views194 slides
2012: ql.io and Node.js by
2012: ql.io and Node.js2012: ql.io and Node.js
2012: ql.io and Node.jsJonathan LeBlanc
3.6K views40 slides
JPA - Beyond copy-paste by
JPA - Beyond copy-pasteJPA - Beyond copy-paste
JPA - Beyond copy-pasteJakub Kubrynski
2K views42 slides
C++ for Java Developers (JavaZone Academy 2018) by
C++ for Java Developers (JavaZone Academy 2018)C++ for Java Developers (JavaZone Academy 2018)
C++ for Java Developers (JavaZone Academy 2018)Patricia Aas
1.4K views56 slides
TDC2016SP - Trilha .NET by
TDC2016SP - Trilha .NETTDC2016SP - Trilha .NET
TDC2016SP - Trilha .NETtdc-globalcode
338 views89 slides

What's hot(12)

Deep dive into Xtext scoping local and global scopes explained by Holger Schill
Deep dive into Xtext scoping local and global scopes explainedDeep dive into Xtext scoping local and global scopes explained
Deep dive into Xtext scoping local and global scopes explained
Holger Schill3.4K views
groovy DSLs from beginner to expert by Paul King
groovy DSLs from beginner to expertgroovy DSLs from beginner to expert
groovy DSLs from beginner to expert
Paul King9.8K views
C++ for Java Developers (JavaZone Academy 2018) by Patricia Aas
C++ for Java Developers (JavaZone Academy 2018)C++ for Java Developers (JavaZone Academy 2018)
C++ for Java Developers (JavaZone Academy 2018)
Patricia Aas1.4K views
Summer of Tech 2017 - Kotlin/Android bootcamp by Kai Koenig
Summer of Tech 2017 - Kotlin/Android bootcampSummer of Tech 2017 - Kotlin/Android bootcamp
Summer of Tech 2017 - Kotlin/Android bootcamp
Kai Koenig939 views
Python Debugging Fundamentals by cbcunc
Python Debugging FundamentalsPython Debugging Fundamentals
Python Debugging Fundamentals
cbcunc1.7K views
Kotlin for android by Ahmed Nabil
Kotlin for androidKotlin for android
Kotlin for android
Ahmed Nabil440 views

Viewers also liked

No More Deadlocks; Asynchronous Programming in .NET by
No More Deadlocks; Asynchronous Programming in .NETNo More Deadlocks; Asynchronous Programming in .NET
No More Deadlocks; Asynchronous Programming in .NETFilip Ekberg
3.1K views23 slides
C# 7.0 Hacks and Features by
C# 7.0 Hacks and FeaturesC# 7.0 Hacks and Features
C# 7.0 Hacks and FeaturesAbhishek Sur
1.2K views60 slides
Asynchronous programming by
Asynchronous programmingAsynchronous programming
Asynchronous programmingFilip Ekberg
45.6K views59 slides
5 Hidden Performance Problems for ASP.NET by
5 Hidden Performance Problems for ASP.NET5 Hidden Performance Problems for ASP.NET
5 Hidden Performance Problems for ASP.NETMatt Watson
1.2K views39 slides
2. java oop by
2. java oop2. java oop
2. java oopVitalify Asia
1.1K views41 slides
C# 6.0 - April 2014 preview by
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewPaulo Morgado
3K views45 slides

Viewers also liked(20)

No More Deadlocks; Asynchronous Programming in .NET by Filip Ekberg
No More Deadlocks; Asynchronous Programming in .NETNo More Deadlocks; Asynchronous Programming in .NET
No More Deadlocks; Asynchronous Programming in .NET
Filip Ekberg3.1K views
C# 7.0 Hacks and Features by Abhishek Sur
C# 7.0 Hacks and FeaturesC# 7.0 Hacks and Features
C# 7.0 Hacks and Features
Abhishek Sur1.2K views
Asynchronous programming by Filip Ekberg
Asynchronous programmingAsynchronous programming
Asynchronous programming
Filip Ekberg45.6K views
5 Hidden Performance Problems for ASP.NET by Matt Watson
5 Hidden Performance Problems for ASP.NET5 Hidden Performance Problems for ASP.NET
5 Hidden Performance Problems for ASP.NET
Matt Watson1.2K views
C# 6.0 - April 2014 preview by Paulo Morgado
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
Paulo Morgado3K views
C# and the Evolution of a Programming Language by Jacinto Limjap
C# and the Evolution of a Programming LanguageC# and the Evolution of a Programming Language
C# and the Evolution of a Programming Language
Jacinto Limjap1.2K views
Switch to Results in Hotels by Jack Watson
Switch to Results in HotelsSwitch to Results in Hotels
Switch to Results in Hotels
Jack Watson724 views
Xamarin 3 hieu 19-06 by Nguyen Hieu
Xamarin 3   hieu 19-06Xamarin 3   hieu 19-06
Xamarin 3 hieu 19-06
Nguyen Hieu1.3K views
Xamarin Cross-Platform with Xamarin.Form, MvvmCross by Tri Nguyen
Xamarin Cross-Platform with Xamarin.Form, MvvmCrossXamarin Cross-Platform with Xamarin.Form, MvvmCross
Xamarin Cross-Platform with Xamarin.Form, MvvmCross
Tri Nguyen1.8K views
C# 4.0 and .NET 4.0 by Buu Nguyen
C# 4.0 and .NET 4.0C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0
Buu Nguyen1.5K views
Enterprise Mobile Success with Oracle and Xamarin by Xamarin
Enterprise Mobile Success with Oracle and XamarinEnterprise Mobile Success with Oracle and Xamarin
Enterprise Mobile Success with Oracle and Xamarin
Xamarin2.5K views
Mobile Enterprise Success with Xamarin and IBM by Xamarin
Mobile Enterprise Success with Xamarin and IBMMobile Enterprise Success with Xamarin and IBM
Mobile Enterprise Success with Xamarin and IBM
Xamarin4.1K views
C# 3.0 and 4.0 by Buu Nguyen
C# 3.0 and 4.0C# 3.0 and 4.0
C# 3.0 and 4.0
Buu Nguyen2.1K views
Mobile Cross-Platform App Development in C# with Xamarin by Nick Landry
Mobile Cross-Platform App Development in C# with XamarinMobile Cross-Platform App Development in C# with Xamarin
Mobile Cross-Platform App Development in C# with Xamarin
Nick Landry2.4K views
Xamarin Mobile Leaders Summit: Business at the Point of Inspiration: Producti... by Xamarin
Xamarin Mobile Leaders Summit: Business at the Point of Inspiration: Producti...Xamarin Mobile Leaders Summit: Business at the Point of Inspiration: Producti...
Xamarin Mobile Leaders Summit: Business at the Point of Inspiration: Producti...
Xamarin913 views
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng... by Xamarin
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...
Xamarin1.1K views
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps by Xamarin
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOpsXamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
Xamarin2K views
Building Your First Xamarin.Forms App by Xamarin
Building Your First Xamarin.Forms AppBuilding Your First Xamarin.Forms App
Building Your First Xamarin.Forms App
Xamarin1.3K views

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

Beauty & the Beast - Java VS TypeScript by
Beauty & the Beast - Java VS TypeScriptBeauty & the Beast - Java VS TypeScript
Beauty & the Beast - Java VS TypeScriptHendrik Ebbers
878 views63 slides
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste by
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-pasteJDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-pastePROIDEA
159 views44 slides
What's new in c# 10 by
What's new in c# 10What's new in c# 10
What's new in c# 10Moaid Hathot
101 views100 slides
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB by
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDBTDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDBtdc-globalcode
439 views82 slides
Lies Told By The Kotlin Compiler by
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerGarth Gilmour
7 views96 slides
Rise of the Machines: PHP and IoT - ZendCon 2017 by
Rise of the Machines: PHP and IoT - ZendCon 2017Rise of the Machines: PHP and IoT - ZendCon 2017
Rise of the Machines: PHP and IoT - ZendCon 2017Colin O'Dell
2.3K views67 slides

Similar to C# 6.0 - What?! C# is being updated?(20)

Beauty & the Beast - Java VS TypeScript by Hendrik Ebbers
Beauty & the Beast - Java VS TypeScriptBeauty & the Beast - Java VS TypeScript
Beauty & the Beast - Java VS TypeScript
Hendrik Ebbers878 views
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste by PROIDEA
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-pasteJDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste
PROIDEA159 views
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB by tdc-globalcode
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDBTDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
tdc-globalcode439 views
Lies Told By The Kotlin Compiler by Garth Gilmour
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
Garth Gilmour7 views
Rise of the Machines: PHP and IoT - ZendCon 2017 by Colin O'Dell
Rise of the Machines: PHP and IoT - ZendCon 2017Rise of the Machines: PHP and IoT - ZendCon 2017
Rise of the Machines: PHP and IoT - ZendCon 2017
Colin O'Dell2.3K views
Firebase & SwiftUI Workshop by Peter Friese
Firebase & SwiftUI WorkshopFirebase & SwiftUI Workshop
Firebase & SwiftUI Workshop
Peter Friese158 views
Data Science Amsterdam - Massively Parallel Processing with Procedural Languages by Ian Huston
Data Science Amsterdam - Massively Parallel Processing with Procedural LanguagesData Science Amsterdam - Massively Parallel Processing with Procedural Languages
Data Science Amsterdam - Massively Parallel Processing with Procedural Languages
Ian Huston1.2K views
C++ Interface Versioning by Skills Matter
C++ Interface VersioningC++ Interface Versioning
C++ Interface Versioning
Skills Matter3.1K views
Introducing PHP Latest Updates by Iftekhar Eather
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
Iftekhar Eather1.3K views
Mercury: A Functional Review by Mark Cheeseman
Mercury: A Functional ReviewMercury: A Functional Review
Mercury: A Functional Review
Mark Cheeseman219 views
Python on Android with Delphi FMX - The Cross Platform GUI Framework by Embarcadero Technologies
Python on Android with Delphi FMX - The Cross Platform GUI Framework Python on Android with Delphi FMX - The Cross Platform GUI Framework
Python on Android with Delphi FMX - The Cross Platform GUI Framework
PHP in one presentation by Milad Rahimi
PHP in one presentationPHP in one presentation
PHP in one presentation
Milad Rahimi530 views

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

  • 1. C# 6.0 What?! C# is Being Updated? Filip Ekberg
  • 2. Page / Copyright ©2014 2 by Readify Pty Ltd @fekberg
  • 3. Page Filip Ekberg C# MVP http://fekberg.com @fekberg C# Smorgasbord / Copyright ©2014 3 by Readify Pty Ltd
  • 5. Page C# 2.0 › Generics › Partial types › Anonymous methods › Iterators › Nullable types › Getter/Setter separate accessibility › Static classes › And more.. / Copyright ©2014 5 by Readify Pty Ltd @fekberg
  • 6. 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 6 by Readify Pty Ltd @fekberg
  • 7. Page C# 4.0 › Dynamic binding › Named and optional parameters › Generic co-and contravariance › And more.. / Copyright ©2014 7 by Readify Pty Ltd @fekberg
  • 8. Page C# 5.0 › Asynchronous methods › Caller info attributes / Copyright ©2014 8 by Readify Pty Ltd @fekberg
  • 9. 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 9 by Readify Pty Ltd @fekberg
  • 10. Page C# 6.0 Overview › Auto-property initializers › Getter-only auto-properties › Assignment to getter-only auto-properties from constructor › Parameter-less struct constructors › Using Statements for Static Members › Dictionary Initializer › Await in catch/finally › Exception filters › Expression-bodied members / Copyright ©2014 10 by Readify Pty Ltd › Null propagation › String interpolation › nameofoperator › And more..
  • 11. Page Auto-Property Initializers / Copyright ©2014 11 by Readify Pty Ltd
  • 12. Page Auto-property initializers in a nutshell class Person { public string Name { get; set; } } class Person { = "Anonymous"; public string Name { get; } = "Anonymous"; } / Copyright ©2014 12 by Readify Pty Ltd @fekberg
  • 13. Page Auto-property initializers in a nutshell class Person { public string Name { get; } public Person() { Name = "Filip"; } } / Copyright ©2014 13 by Readify Pty Ltd @fekberg
  • 14. Page Auto-property initializers in a nutshell class Person { private readonly string <Name>k__BackingField = "Filip"; public string Name { get { return this.<Name>k__BackingField; } } } / Copyright ©2014 14 by Readify Pty Ltd @fekberg
  • 15. Page Parameter-less struct constructors / Copyright ©2014 15 by Readify Pty Ltd
  • 16. Page Parameter-less struct constructors in a nutshell struct Point { public int X { get; } public int Y { get; } } Read Only! public Point() { X = 100; Y = 100; } / Copyright ©2014 16 by Readify Pty Ltd @fekberg
  • 17. Page Parameter-less struct constructors in a nutshell 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; } } / Copyright ©2014 17 by Readify Pty Ltd @fekberg
  • 18. Page Parameter-less struct constructors in a nutshell 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) { } } / Copyright ©2014 18 by Readify Pty Ltd @fekberg
  • 19. Page Using Statements for Static Members / Copyright ©2014 19 by Readify Pty Ltd
  • 20. Page Using Statements for Static Members in a nutshell class Program { static void Main(string[] args) { var angle = 90d; Console.WriteLine(Math.Sin(angle)); } } / Copyright ©2014 20 by Readify Pty Ltd @fekberg
  • 21. Page Using Statements for Static Members in a nutshell using System.Console; using System.Math; class Program { static void Main(string[] args) { var angle = 90d; WriteLine(Sin(angle)); } } / Copyright ©2014 21 by Readify Pty Ltd @fekberg
  • 22. Page Using Statements for Static Members in a nutshell using System.Console; using System.Linq.Enumerable; class Program { static void Main(string[] args) { foreach (var i in Range(0, 10)) { WriteLine(i); } } } / Copyright ©2014 22 by Readify Pty Ltd @fekberg
  • 23. Page Dictionary Initializers / Copyright ©2014 23 by Readify Pty Ltd
  • 24. Page Dictionary Initializers in a nutshell var people = new Dictionary<string, Person> { ["Filip"] = new Person() }; var answers = new Dictionary<int, string> { [42] = "the answer to life the universe and everything" }; / Copyright ©2014 24 by Readify Pty Ltd @fekberg
  • 25. Page Await inside Finally block / Copyright ©2014 25 by Readify Pty Ltd
  • 26. Page Await + Finally in a nutshell public async Task DownloadAsync() { } try { } catch { await Task.Delay(2000); } finally { await Task.Delay(2000); } / Copyright ©2014 26 by Readify Pty Ltd @fekberg
  • 27. Page Exception Filters / Copyright ©2014 27 by Readify Pty Ltd
  • 28. Page Exception filters in a nutshell 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!"); } / Copyright ©2014 28 by Readify Pty Ltd @fekberg
  • 29. Page Null Propagation / Copyright ©2014 29 by Readify Pty Ltd
  • 30. Page Null propagation in a nutshell class Person { public string Name { get; set; } public Address Address { get; set; } } class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } } var filip = new Person { Name = "Filip" }; Console.WriteLine(filip.Address.AddressLine1); / Copyright ©2014 30 by Readify Pty Ltd @fekberg
  • 31. Page Null propagation in a nutshell class Person { public string Name { get; set; } public Address Address { get; set; } } class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } } var filip = new Person { Name = "Filip" }; Console.WriteLine(filip.Address.AddressLine1); / Copyright ©2014 31 by Readify Pty Ltd @fekberg
  • 32. Page Null propagation in a nutshell class Person { public string Name { get; set; } public Address Address { get; set; } } class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } } var filip = new Person { Name = "Filip" }; Console.WriteLine(filip.Address .=A=d dnruelslsL?i n"eN1o) ;Address" : filip.Address.AddressLine1); / Copyright ©2014 32 by Readify Pty Ltd @fekberg
  • 33. Page Null propagation in a nutshell class Person { public string Name { get; set; } public Address Address { get; set; } } class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } } var filip = new Person { Name = "Filip" }; Console.WriteLine(filip.Address .=A=d dnruelslsL?i n"eN1o) ;Address" : filip.Address.AddressLine1); Console.WriteLine(filip?.Address?.AddressLine1 ?? "No Address"); / Copyright ©2014 33 by Readify Pty Ltd @fekberg
  • 34. Page Null propagation in a nutshell var people = new[] { new Person(), null }; WriteLine(people[0]?.Name); WriteLine(people[1]?.Name); / Copyright ©2014 34 by Readify Pty Ltd @fekberg
  • 35. Page Null propagation in a nutshell Person[] people = null; WriteLine(people?[0]?.Name); Person[] people = null; Console.WriteLine( (people != null) ? ((people[0] == null) ? null : people[0].Name) : null ); / Copyright ©2014 35 by Readify Pty Ltd @fekberg
  • 36. Page Expression-bodied members / Copyright ©2014 36 by Readify Pty Ltd
  • 37. Page Expression-bodied members in a nutshell class Rectangle { public double Width { get; set; } public double Height { get; set; } public double Area => Width * Height; } / Copyright ©2014 37 by Readify Pty Ltd @fekberg
  • 38. Page Expression-bodied members in a nutshell 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}"; } / Copyright ©2014 38 by Readify Pty Ltd @fekberg
  • 39. Page String interpolation / Copyright ©2014 39 by Readify Pty Ltd
  • 40. Page String interpolation in a nutshell public override string ToString() => "My Width is {Width} and my Height is {Height}"; Syntax will change in a later release to the following: public override string ToString() => $"My Width is {Width} and my Height is {Height}"; / Copyright ©2014 40 by Readify Pty Ltd @fekberg
  • 41. Page String interpolation in a nutshell 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); } / Copyright ©2014 41 by Readify Pty Ltd @fekberg
  • 42. int num = 28; object[] objArray1 = new object[] { num }; Console.WriteLine(string.Format("Hello there, I'm {0:D5} years old!", objArray1)); Page String interpolation in a nutshell int age = 28; var result = "Hello there, I'm {age : D5} years old!"; WriteLine(result); / Copyright ©2014 42 by Readify Pty Ltd @fekberg
  • 43. Page nameof operator / Copyright ©2014 43 by Readify Pty Ltd
  • 44. Page nameof operator in a nutshell static void Main(string[] args) { WriteLine("Parameter name is: {nameof(args)}"); } / Copyright ©2014 44 by Readify Pty Ltd @fekberg
  • 45. Page nameof operator in a nutshell public double CalculateArea(int width, int height) { if (width <= 0) { throw new ArgumentException("Parameter {nameof(width)} cannot be less than 0"); } return width * height; } / Copyright ©2014 45 by Readify Pty Ltd @fekberg
  • 46. Page Demo C# 6.0 Language Features in Visual Studio 2015 / Copyright ©2014 46 by Readify Pty Ltd
  • 47. Page There’s more?? / Copyright ©2014 47 by Readify Pty Ltd
  • 48. Page What about C# 7.0? › Binary literals and Digit separators / Copyright ©2014 48 by Readify Pty Ltd 0b00001000 0xFF_00_FA_AF
  • 49. Page What about C# 7.0? › Binary literals and Digit separators › Event initializers / Copyright ©2014 49 by Readify Pty Ltd var client = new WebClient { DownloadFileCompleted += DownloadFileCompletedHandler };
  • 50. Page What about C# 7.0? › Binary literals and Digit separators › Event initializers › Field targets on auto-properties / Copyright ©2014 50 by Readify Pty Ltd [field: NonSerialized] public int Age { get; set; }
  • 51. Page What about C# 7.0? › Binary literals and Digit separators › Event initializers › Field targets on auto-properties › Semicolon operator / Copyright ©2014 51 by Readify Pty Ltd var y = (var x = Foo(); Write(x); x * x);
  • 52. 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 52 by Readify Pty Ltd int Avg(params IEnumerable<int> numbers)
  • 53. 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 53 by Readify Pty Ltd public void CalculateAgeBasedOn(int birthYear, out int age) { age = DateTime.Now.Year - birthYear; } CalculateAgeBasedOn(1987, out var age);
  • 54. 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 54 by Readify Pty Ltd class Person(string name, int age) { private string _name = name; private int _age = age; }
  • 55. Page More with Roslyn › Exposes the CompilerAPIs › Plugins powered by Roslyn › Analyze code › Re-write code / Copyright ©2014 55 by Readify Pty Ltd @fekberg
  • 56. Page Summary › 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 / Copyright ©2014 56 by Readify Pty Ltd @fekberg
  • 57. Page Questions? / Copyright ©2014 57 by Readify Pty Ltd
  • 58. Page Thank you! http://fekberg.com @fekberg C# Smorgasbord / Copyright ©2014 58 by Readify Pty Ltd