C# 6.0 New Features
Mykhaylo Stepanyak
SoftServe, 2015
Agenda
 C# Evolution
 .NET Compiler Platform (“Roslyn”)
 C# 6.0 Features
 Demo
C# Evolution
C# 1.0
Managed
C# 2.0
Generics
C# 3.0
LINQ
C# 6.0
Compiler Platform
“Roslyn”
C# 5.0
Asynchronous
programming
C# 4.0
Dynamic
programming
.NET Compiler Platform (“Roslyn”)
Building compilers and related tools
 Code emitter
 Object model for trees
 .NET Compiler Platform
 Code name “Roslyn”
 Rewrite of the C# and Visual Basic compilers
 Exhaustive set of libraries for
 Code analysis of managed languages
• C#
• Visual Basic
 Language services integrated in Visual Studio
• Refactoring
• IntelliSense and code browsing
Why Microsoft Built “Roslyn”
Faster turn-around to build new language features
 Support experimentation and prototyping
• E.g. C# 6.0 has a bunch of small features
 Comprehensive set of rich APIs
• Syntax trees and visitors
• Semantic analysis
• Compilation model
 Lights up in all compiler contexts at once
• Richer tooling can be built
Openness and ecosystem
 Allow 3rdparties to build extensions
• Democratize refactorings and other tools
• Eliminate private APIs for Visual Studio
 Education about compiler internals
 Open source
What’s inside?
Compilation Steps
Compilation Steps
Compilation Steps
Compilation Steps
Compliers APIs
Language Services
C# 6.0 Features
Auto-property enhancements
public class Customer
{
public string First { get; set; } = "Jane";
public string Last { get; set; } = "Doe";
}
public class Customer
{
public string First { get; } = "Jane";
public string Last { get; } = "Doe";
}
Primary constructors
public class Customer(string first, string last)
{
public string First { get; } = first;
public string Last { get; } = last;
}
Using static
using System.Console;
using System.Math;
class Program
{
static void Main()
{
WriteLine(Sqrt(3*3 + 4*4));
}
}
String interpolation
var s = "{p.Name} is {p.Age} year{s} old";
var s = "{p.Name} is {p.Age} year{(p.Age == 1 ? "" : "s")} old";
Expression-bodied function members
public Point Move(int dx, int dy) => new Point(x + dx, y + dy);
public static Complex operator +(Complex a, Complex b) => a.Add(b);
public static implicit operator string(Person p) => "{p.First} {p.Last}";
Declaration expressions
if (int.TryParse(s, out int i)) { … }
GetCoordinates(out var x, out var y);
Console.WriteLine("Result: {0}", (int x = GetValue()) * x);
if ((string s = o as string) != null) { … s … }
Exception filters
try { … }
catch (MyException e) if (myfilter(e))
{
…
}
Binary literals and digit separators (Try them
out in VB)
var bits = 0b00101110;
var bits = 0b0010_1110;
var hex = 0x00_2E;
var dec = 1_234_567_890;
Indexed members and element initializers
var numbers = new Dictionary<int, string> {
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};
var customer = new JsonData {
$first = "Anders", // => ["first"] = "Anders"
$last = "Hejlsberg" // => ["last"] = "Hejlsberg"
}
string first = customer.$first; // => customer["first"]
Await in catch and finally blocks
Resource res = null;
try
{
res = await Resource.OpenAsync(…); // You could do this.
…
}
catch(ResourceException e)
{
await Resource.LogAsync(res, e); // Now you can do this …
}
finally
{
if (res != null) await res.CloseAsync(); // … and this.
}
Null-conditional operators
int? length = customers?.Length; // null if customers is null
Customer first = customers?[0]; // null if customers is null
int? first = customers?[0].Orders.Count(); // if customers is null, the result is null
Demo
Mykhaylo Stepanyak
mstepanyak@gmail.com
Lviv, Ukraine
Contacts

Roslyn and C# 6.0 New Features

  • 1.
    C# 6.0 NewFeatures Mykhaylo Stepanyak SoftServe, 2015
  • 2.
    Agenda  C# Evolution .NET Compiler Platform (“Roslyn”)  C# 6.0 Features  Demo
  • 3.
    C# Evolution C# 1.0 Managed C#2.0 Generics C# 3.0 LINQ C# 6.0 Compiler Platform “Roslyn” C# 5.0 Asynchronous programming C# 4.0 Dynamic programming
  • 4.
    .NET Compiler Platform(“Roslyn”) Building compilers and related tools  Code emitter  Object model for trees  .NET Compiler Platform  Code name “Roslyn”  Rewrite of the C# and Visual Basic compilers  Exhaustive set of libraries for  Code analysis of managed languages • C# • Visual Basic  Language services integrated in Visual Studio • Refactoring • IntelliSense and code browsing
  • 5.
    Why Microsoft Built“Roslyn” Faster turn-around to build new language features  Support experimentation and prototyping • E.g. C# 6.0 has a bunch of small features  Comprehensive set of rich APIs • Syntax trees and visitors • Semantic analysis • Compilation model  Lights up in all compiler contexts at once • Richer tooling can be built Openness and ecosystem  Allow 3rdparties to build extensions • Democratize refactorings and other tools • Eliminate private APIs for Visual Studio  Education about compiler internals  Open source
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
    Auto-property enhancements public classCustomer { public string First { get; set; } = "Jane"; public string Last { get; set; } = "Doe"; } public class Customer { public string First { get; } = "Jane"; public string Last { get; } = "Doe"; }
  • 15.
    Primary constructors public classCustomer(string first, string last) { public string First { get; } = first; public string Last { get; } = last; }
  • 16.
    Using static using System.Console; usingSystem.Math; class Program { static void Main() { WriteLine(Sqrt(3*3 + 4*4)); } }
  • 17.
    String interpolation var s= "{p.Name} is {p.Age} year{s} old"; var s = "{p.Name} is {p.Age} year{(p.Age == 1 ? "" : "s")} old";
  • 18.
    Expression-bodied function members publicPoint Move(int dx, int dy) => new Point(x + dx, y + dy); public static Complex operator +(Complex a, Complex b) => a.Add(b); public static implicit operator string(Person p) => "{p.First} {p.Last}";
  • 19.
    Declaration expressions if (int.TryParse(s,out int i)) { … } GetCoordinates(out var x, out var y); Console.WriteLine("Result: {0}", (int x = GetValue()) * x); if ((string s = o as string) != null) { … s … }
  • 20.
    Exception filters try {… } catch (MyException e) if (myfilter(e)) { … }
  • 21.
    Binary literals anddigit separators (Try them out in VB) var bits = 0b00101110; var bits = 0b0010_1110; var hex = 0x00_2E; var dec = 1_234_567_890;
  • 22.
    Indexed members andelement initializers var numbers = new Dictionary<int, string> { [7] = "seven", [9] = "nine", [13] = "thirteen" }; var customer = new JsonData { $first = "Anders", // => ["first"] = "Anders" $last = "Hejlsberg" // => ["last"] = "Hejlsberg" } string first = customer.$first; // => customer["first"]
  • 23.
    Await in catchand finally blocks Resource res = null; try { res = await Resource.OpenAsync(…); // You could do this. … } catch(ResourceException e) { await Resource.LogAsync(res, e); // Now you can do this … } finally { if (res != null) await res.CloseAsync(); // … and this. }
  • 24.
    Null-conditional operators int? length= customers?.Length; // null if customers is null Customer first = customers?[0]; // null if customers is null int? first = customers?[0].Orders.Count(); // if customers is null, the result is null
  • 25.
  • 26.