SlideShare a Scribd company logo
1 of 58
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

More Related Content

What's hot

Deep dive into Xtext scoping local and global scopes explained
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
 
groovy DSLs from beginner to expert
groovy DSLs from beginner to expertgroovy DSLs from beginner to expert
groovy DSLs from beginner to expertPaul King
 
C++ for Java Developers (JavaZone Academy 2018)
C++ for Java Developers (JavaZone Academy 2018)C++ for Java Developers (JavaZone Academy 2018)
C++ for Java Developers (JavaZone Academy 2018)Patricia Aas
 
TDC2016SP - Trilha .NET
TDC2016SP - Trilha .NETTDC2016SP - Trilha .NET
TDC2016SP - Trilha .NETtdc-globalcode
 
En story of cakephp2.0
En story of cakephp2.0En story of cakephp2.0
En story of cakephp2.0Hiroki Shimizu
 
Evolving The Java Language
Evolving The Java LanguageEvolving The Java Language
Evolving The Java LanguageQConLondon2008
 
Summer of Tech 2017 - Kotlin/Android bootcamp
Summer of Tech 2017 - Kotlin/Android bootcampSummer of Tech 2017 - Kotlin/Android bootcamp
Summer of Tech 2017 - Kotlin/Android bootcampKai Koenig
 
Python Debugging Fundamentals
Python Debugging FundamentalsPython Debugging Fundamentals
Python Debugging Fundamentalscbcunc
 
Kotlin for android
Kotlin for androidKotlin for android
Kotlin for androidAhmed Nabil
 

What's hot (12)

Deep dive into Xtext scoping local and global scopes explained
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
 
groovy DSLs from beginner to expert
groovy DSLs from beginner to expertgroovy DSLs from beginner to expert
groovy DSLs from beginner to expert
 
2012: ql.io and Node.js
2012: ql.io and Node.js2012: ql.io and Node.js
2012: ql.io and Node.js
 
JPA - Beyond copy-paste
JPA - Beyond copy-pasteJPA - Beyond copy-paste
JPA - Beyond copy-paste
 
C++ for Java Developers (JavaZone Academy 2018)
C++ for Java Developers (JavaZone Academy 2018)C++ for Java Developers (JavaZone Academy 2018)
C++ for Java Developers (JavaZone Academy 2018)
 
TDC2016SP - Trilha .NET
TDC2016SP - Trilha .NETTDC2016SP - Trilha .NET
TDC2016SP - Trilha .NET
 
En story of cakephp2.0
En story of cakephp2.0En story of cakephp2.0
En story of cakephp2.0
 
Evolving The Java Language
Evolving The Java LanguageEvolving The Java Language
Evolving The Java Language
 
Summer of Tech 2017 - Kotlin/Android bootcamp
Summer of Tech 2017 - Kotlin/Android bootcampSummer of Tech 2017 - Kotlin/Android bootcamp
Summer of Tech 2017 - Kotlin/Android bootcamp
 
Python Debugging Fundamentals
Python Debugging FundamentalsPython Debugging Fundamentals
Python Debugging Fundamentals
 
Polyglot JVM
Polyglot JVMPolyglot JVM
Polyglot JVM
 
Kotlin for android
Kotlin for androidKotlin for android
Kotlin for android
 

Viewers also liked

No More Deadlocks; Asynchronous Programming in .NET
No More Deadlocks; Asynchronous Programming in .NETNo More Deadlocks; Asynchronous Programming in .NET
No More Deadlocks; Asynchronous Programming in .NETFilip Ekberg
 
C# 7.0 Hacks and Features
C# 7.0 Hacks and FeaturesC# 7.0 Hacks and Features
C# 7.0 Hacks and FeaturesAbhishek Sur
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programmingFilip Ekberg
 
5 Hidden Performance Problems for ASP.NET
5 Hidden Performance Problems for ASP.NET5 Hidden Performance Problems for ASP.NET
5 Hidden Performance Problems for ASP.NETMatt Watson
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewPaulo Morgado
 
C# and the Evolution of a Programming Language
C# and the Evolution of a Programming LanguageC# and the Evolution of a Programming Language
C# and the Evolution of a Programming LanguageJacinto Limjap
 
Switch to Results in Hotels
Switch to Results in HotelsSwitch to Results in Hotels
Switch to Results in HotelsJack Watson
 
Xamarin 3 hieu 19-06
Xamarin 3   hieu 19-06Xamarin 3   hieu 19-06
Xamarin 3 hieu 19-06Nguyen Hieu
 
Xamarin Cross-Platform with Xamarin.Form, MvvmCross
Xamarin Cross-Platform with Xamarin.Form, MvvmCrossXamarin Cross-Platform with Xamarin.Form, MvvmCross
Xamarin Cross-Platform with Xamarin.Form, MvvmCrossTri Nguyen
 
C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0Buu Nguyen
 
Enterprise Mobile Success with Oracle and Xamarin
Enterprise Mobile Success with Oracle and XamarinEnterprise Mobile Success with Oracle and Xamarin
Enterprise Mobile Success with Oracle and XamarinXamarin
 
Mobile Enterprise Success with Xamarin and IBM
Mobile Enterprise Success with Xamarin and IBMMobile Enterprise Success with Xamarin and IBM
Mobile Enterprise Success with Xamarin and IBMXamarin
 
C# 3.0 and 4.0
C# 3.0 and 4.0C# 3.0 and 4.0
C# 3.0 and 4.0Buu Nguyen
 
Mobile Cross-Platform App Development in C# with Xamarin
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 XamarinNick Landry
 
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...
Xamarin Mobile Leaders Summit: Business at the Point of Inspiration: Producti...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...
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...Xamarin
 
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
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 DevOpsXamarin
 
Building Your First Xamarin.Forms App
Building Your First Xamarin.Forms AppBuilding Your First Xamarin.Forms App
Building Your First Xamarin.Forms AppXamarin
 

Viewers also liked (20)

No More Deadlocks; Asynchronous Programming in .NET
No More Deadlocks; Asynchronous Programming in .NETNo More Deadlocks; Asynchronous Programming in .NET
No More Deadlocks; Asynchronous Programming in .NET
 
C# 7.0 Hacks and Features
C# 7.0 Hacks and FeaturesC# 7.0 Hacks and Features
C# 7.0 Hacks and Features
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
 
5 Hidden Performance Problems for ASP.NET
5 Hidden Performance Problems for ASP.NET5 Hidden Performance Problems for ASP.NET
5 Hidden Performance Problems for ASP.NET
 
2. java oop
2. java oop2. java oop
2. java oop
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
 
What's new in C# 6.0
What's new in C# 6.0What's new in C# 6.0
What's new in C# 6.0
 
C# and the Evolution of a Programming Language
C# and the Evolution of a Programming LanguageC# and the Evolution of a Programming Language
C# and the Evolution of a Programming Language
 
Switch to Results in Hotels
Switch to Results in HotelsSwitch to Results in Hotels
Switch to Results in Hotels
 
Xamarin 3 hieu 19-06
Xamarin 3   hieu 19-06Xamarin 3   hieu 19-06
Xamarin 3 hieu 19-06
 
Xamarin Cross-Platform with Xamarin.Form, MvvmCross
Xamarin Cross-Platform with Xamarin.Form, MvvmCrossXamarin Cross-Platform with Xamarin.Form, MvvmCross
Xamarin Cross-Platform with Xamarin.Form, MvvmCross
 
C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0
 
Enterprise Mobile Success with Oracle and Xamarin
Enterprise Mobile Success with Oracle and XamarinEnterprise Mobile Success with Oracle and Xamarin
Enterprise Mobile Success with Oracle and Xamarin
 
Mobile Enterprise Success with Xamarin and IBM
Mobile Enterprise Success with Xamarin and IBMMobile Enterprise Success with Xamarin and IBM
Mobile Enterprise Success with Xamarin and IBM
 
C# 3.0 and 4.0
C# 3.0 and 4.0C# 3.0 and 4.0
C# 3.0 and 4.0
 
Mobile Cross-Platform App Development in C# with Xamarin
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
 
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...
Xamarin Mobile Leaders Summit: Business at the Point of Inspiration: Producti...
 
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...
Xamarin Mobile Leaders Summit: The Mobile Mind Shift: Opportunities, Challeng...
 
Xamarin Mobile Leaders Summit | Solving the Unique Challenges in Mobile DevOps
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
 
Building Your First Xamarin.Forms App
Building Your First Xamarin.Forms AppBuilding Your First Xamarin.Forms App
Building Your First Xamarin.Forms App
 

Similar to C# 6.0 Language Features Explained

Beauty & the Beast - Java VS TypeScript
Beauty & the Beast - Java VS TypeScriptBeauty & the Beast - Java VS TypeScript
Beauty & the Beast - Java VS TypeScriptHendrik Ebbers
 
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-pasteJDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-pastePROIDEA
 
What's new in c# 10
What's new in c# 10What's new in c# 10
What's new in c# 10Moaid Hathot
 
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
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
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerGarth Gilmour
 
Rise of the Machines: PHP and IoT - ZendCon 2017
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
 
Firebase & SwiftUI Workshop
Firebase & SwiftUI WorkshopFirebase & SwiftUI Workshop
Firebase & SwiftUI WorkshopPeter Friese
 
Data Science Amsterdam - Massively Parallel Processing with Procedural Languages
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 LanguagesIan Huston
 
C++ Interface Versioning
C++ Interface VersioningC++ Interface Versioning
C++ Interface VersioningSkills Matter
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityGeorgePeterBanyard
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest UpdatesIftekhar Eather
 
Mercury: A Functional Review
Mercury: A Functional ReviewMercury: A Functional Review
Mercury: A Functional ReviewMark Cheeseman
 
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
Python on Android with Delphi FMX - The Cross Platform GUI Framework Embarcadero Technologies
 
PHP in one presentation
PHP in one presentationPHP in one presentation
PHP in one presentationMilad Rahimi
 

Similar to C# 6.0 Language Features Explained (20)

Beauty & the Beast - Java VS TypeScript
Beauty & the Beast - Java VS TypeScriptBeauty & the Beast - Java VS TypeScript
Beauty & the Beast - Java VS TypeScript
 
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-pasteJDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste
 
What's new in c# 10
What's new in c# 10What's new in c# 10
What's new in c# 10
 
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
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
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
Rise of the Machines: PHP and IoT - ZendCon 2017
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
 
Firebase & SwiftUI Workshop
Firebase & SwiftUI WorkshopFirebase & SwiftUI Workshop
Firebase & SwiftUI Workshop
 
Data Science Amsterdam - Massively Parallel Processing with Procedural Languages
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
 
The state of DI - DPC12
The state of DI - DPC12The state of DI - DPC12
The state of DI - DPC12
 
Den bavp
Den bavpDen bavp
Den bavp
 
Bavp sd
Bavp sdBavp sd
Bavp sd
 
Tf bavp
Tf bavpTf bavp
Tf bavp
 
From Java to Python
From Java to PythonFrom Java to Python
From Java to Python
 
C++ Interface Versioning
C++ Interface VersioningC++ Interface Versioning
C++ Interface Versioning
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
Mercury: A Functional Review
Mercury: A Functional ReviewMercury: A Functional Review
Mercury: A Functional Review
 
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
Python on Android with Delphi FMX - The Cross Platform GUI Framework
 
Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
 
PHP in one presentation
PHP in one presentationPHP in one presentation
PHP in one presentation
 

C# 6.0 Language Features Explained

  • 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