C# 6.0 will change the way you write C#. There are many language features that are so much more efficient you’ll wonder why they weren’t there since the beginning.
7. History
• 2002 - C# 1.0 released with .NET 1.0 and VS2002
• 2003 - C# 1.2 released with .NET 1.1 and VS2003
• First version to call Dispose on IEnumerators which
implemented IDisposable. A few other small features.
• 2005 - C# 2.0 released with .NET 2.0 and VS2005
• Generics, anonymous methods, nullable types, iterator blocks
Credit to Jon Skeet - http://stackoverflow.com/a/247623
8. History
• 2007 - C# 3.0 released with .NET 3.5 and VS2008
• Lambda expressions, extension methods, expression trees, anonymous types,
implicit typing (var), query expressions
• 2010 - C# 4.0 released with .NET 4 and VS2010
• Late binding (dynamic), delegate and interface generic variance, more COM
support, named arguments, tuple data type and optional parameters
• 2012 - C# 5.0 released with .NET 4.5 and VS2012
• Async programming, caller info attributes. Breaking change: loop variable closure.
10. 1. Auto Property Initializers
Join the Conversation #DevSuperPowers @EricPhan
11. Currently
public class Developer
{
public Developer()
{
DrinksCoffee = true;
}
public bool DrinksCoffee { get; set; }
}
C# 6.0
public class Developer
{
public bool DrinksCoffee { get; set; } = true;
}
Auto Property Initializers
Join the Conversation #DevSuperPowers @EricPhan
12. Auto Property Initializers
• Puts the defaults along with the declaration
• Cleans up the constructor
Join the Conversation #DevSuperPowers @EricPhan
14. When I was a kid…
var devSkills =
new Dictionary<string, bool>();
devSkills["C#"] = true;
devSkills["VB.NET"] = true;
devSkills["AngularJS"] = true;
devSkills["Angular2"] = false;
Dictionary Initializers
VS2013 C# 5.0
var devSkills = new
Dictionary<string, bool>()
{
{ "C#", true },
{ "VB.NET", true },
{ "AngularJS", true },
{ "Angular2", false},
};
VS2015 C# 6.0
var devSkills =
new Dictionary<string, bool>()
{
["C#"] = true,
["VB.NET"] = true,
["AngularJS"] = true,
["Angular2"] = false
};
Join the Conversation #DevSuperPowers @EricPhan
15. Dictionary Initializers
• Cleaner, less ‘{‘ and ‘}’ to try and match
• More like how you would actually use a Dictionary
• devSkills["C#"] = true;
Join the Conversation #DevSuperPowers @EricPhan
17. Bad:
var firstName = "Eric";
var lastName = "Phan";
var jobTitle = "Chief Architect";
var company = "SSW";
var display = firstName + " " + lastName + ", " + jobTitle + " @ " + company;
String Interpolation
Better:
var firstName = "Eric";
var lastName = "Phan";
var jobTitle = "Chief Architect";
var company = "SSW";
var display = string.Format("{0} {1}, {3} @ {2}", firstName, lastName, company, jobTitle);
Join the Conversation #DevSuperPowers @EricPhan
18. String Interpolation
• Much clearer as you can see what variables are being
used inline
• Can do formatting inline too
• {startDate:dd/MMM/yy}
• {totalAmount:c2}
Join the Conversation #DevSuperPowers @EricPhan
20. public string[] GenerateCoffeeOrder(Coffee[] favCoffee)
{
var order = favCoffee.Select(x => $"{x.Name} - {x.Size}").ToArray();
return order;
}
Null Conditional Operator
What’s wrong with the above code?
21. public string[] GenerateCoffeeOrder(Coffee[] favCoffee)
{
if (favCoffee == null)
{
return null;
}
var order = favCoffee.Select(x => $"{x.Name} - {x.Size}").ToArray();
return order;
}
C# 6.0
public string[] GenerateCoffeeOrder(Coffee[] favCoffee)
{
var order = favCoffee?.Select(x => $"{x.Name} - {x.Size}").ToArray();
return order;
}
Null Conditional Operator
Fix by adding a null check
Join the Conversation #DevSuperPowers @EricPhan
22. Null Conditional Operator
• <Variable>?.<SomePropertyOrMethod>
• “?” If the variable is null, then return null
• Otherwise execute whatever is after the “.”
• Similar to the ternary operation (conditional operator)
• var tax = hasGST
? subtotal * 1.1
: subtotal;
• Saves you many lines of null checking with just one simple character.
Join the Conversation #DevSuperPowers @EricPhan
25. nameOf Expression
• In the previous example, it would probably be better
to throw an exception if the parameter was null
Join the Conversation #DevSuperPowers @EricPhan
public string[] GenerateCoffeeOrder(Coffee[] favCoffee)
{
if (favCoffee == null)
{
throw new ArgumentNullException("favCoffee");
}
var order = favCoffee.Select(x => $"{x.Name} - {x.Size}").ToArray();
return order;
}
26. nameOf Expression
• What’s the problem?
• Refactor name change
• Change favCoffee to coffeeOrders
Join the Conversation #DevSuperPowers @EricPhan
public string[] GenerateCoffeeOrder(Coffee[] coffeeOrders)
{
if (coffeeOrders == null)
{
throw new ArgumentNullException("favCoffee");
}
var order = coffeeOrders => $"{x.Name} - {x.Size}").ToArray();
return order;
}
Not Renamed!
27. nameOf Expression
Join the Conversation #DevSuperPowers @EricPhan
C# 6.0
public string[] GenerateCoffeeOrder(Coffee[] coffeeOrders)
{
if (coffeeOrders == null)
{
throw new ArgumentNullException(nameOf(coffeeOrders));
}
var order = favCoffee.Select(x => $"{x.Name} - {x.Size}").ToArray();
return order;
}
28. nameOf Expression
• Gives you compile time checking whenever you need
to refer to the name of a parameter
Join the Conversation #DevSuperPowers @EricPhan
30. Join the Conversation #DevSuperPowers @EricPhan
Expression Bodied Functions & Properties
Currently
public class Coffee {
public string Name { get; set; }
public string Size { get; set; }
public string OrderDescription {
get {
return string.Format("{0} - {1}", Name, Size);
}
}
}
C# 6.0
public class Coffee {
public string Name { get; set; }
public string Size { get; set; }
public string OrderDescription => $"{Name} - {Size}";
}
31. C# 6.0
public class Coffee {
public string Name { get; set; }
public string Size { get; set; }
public string OrderDescription => $"{Name} - {Size}";
}
Join the Conversation #DevSuperPowers @EricPhan
Expression Bodied Functions & Properties
public class Coffee {
name: string;
size: string;
orderDescription: () => string;
constructor() {
this.orderDescription = () => `${this.name} - ${this.size}`;
}
}
TypeScript
What language is this?
34. Exception Filters
• Gets rid of all the if statements
• Nice contained blocks to handle specific exception cases
Join the Conversation #DevSuperPowers @EricPhan
Hot Tip: Special peaceful weekends exception filter*
catch (SqlException ex) when (HttpContext.Current.Identity.Name == "Boss Man"
|| DateTime.Now.DayOfWeek == DayOfWeek.Saturday
|| DateTime.Now.DayOfWeek == DayOfWeek.Sunday) {
// Do nothing
} * Disclaimer: Don’t do this
36. C# 6.0
using static System.Math;
public double GetArea(double radius)
{
return PI * Pow(radius, 2);
}
Currently
public double GetArea(double radius)
{
return Math.PI * Math.Pow(radius, 2);
}
Static Using
Join the Conversation #DevSuperPowers @EricPhan
37. Static Using
• Gets rid of the duplicate static class namespace
• Cleaner code
• Like using the With in VB.NET
Join the Conversation #DevSuperPowers @EricPhan
38. There’s more too…
Await in Catch and Finally Blocks
Extension Add in Collection Initializers
Join the Conversation #DevSuperPowers @EricPhan
40. Summary
• Start using these features now!
• VS 2013 + “Install-Package Microsoft.Net.Compilers”
• VS 2015 – Out of the box
• For Build server – Microsoft Build Tools 2015 or
reference the NuGet Package
41. With all these goodies…
• You should be 350% more productive at coding
• Get reminded about using these cool features with
ReSharper