45

Click here to load reader

C# 6.0 - April 2014 preview

Embed Size (px)

Citation preview

Page 1: C# 6.0 - April 2014 preview

C# 6.0 April 2014 Preview Paulo Morgado

http://netponto.org47ª Reunião Lisboa - 31/05/2014

Page 3: C# 6.0 - April 2014 preview

Agenda• C# Evolution• .NET Compiler Platform (“Roslyn”)• C# 6.0 Features• Demos

Page 4: C# 6.0 - April 2014 preview

C# 1.0Managed

C# 2.0Generics

C# 3.0LINQ

C# 4.0Dynamic

Programming

C# 5.0Asynchronous Programming

Windows Runtime

C# 6.0Compiler Platform

“Roslyn”

C# Evolution

Page 5: C# 6.0 - April 2014 preview

.NET Compiler Platform (“Roslyn”)

A reimplementation of C# and VB compilersIn C# and VBWith rich public APIsOn CodePlex

Page 6: C# 6.0 - April 2014 preview

Why “Roslyn”?

TeamClean architecture to evolve on

PartnersSource-based tools and extensions

DevelopersRicher C# IDE experience

OSS

1,000,000s

1,000s

10s

Page 7: C# 6.0 - April 2014 preview

C# 6.0 Features

Disclaimer:At this point, some of the features are implemente, others are planned and others are being considered.

Page 8: C# 6.0 - April 2014 preview

public class Customer{ public string FirstName { get; private set; } public string LastName { get; private set; }}

public class Customer{ public string FirstName { get; private set; } = "John"; public string LastName { get; private set; } = "Doe";}

Auto-Property Initializers

public class Customer{ public string FirstName { get; } = "John"; public string LastName { get; } = "Doe";}

Page 9: C# 6.0 - April 2014 preview

Primary ConstructorsClass and Struct Parameters

public class Customer{    private string firstName;    private string lastName;    public Customer(string firstName, string lastName)    {        this.nome = firstName;        this.lastName = lastName;    }    public string FirstName { get { return firstName; } }    public string LastName { get { return lastName; } }}

public class Customer{    private string firstName;    private string lastName;    public Customer(string firstName, string lastName)    {        this.nome = firstName;        this.lastName = lastName;    }    public string FirstName { get { return firstName; } }    public string LastName { get { return lastName; } }}

public class Customer(string firstName, string lastName){ public string FirstName { get; } = firstName; public string LatName { get; } = lastName;}

Page 10: C# 6.0 - April 2014 preview

Primary ConstructorsField Parameters

public class Customer( string firstName, string lastName,  DateTime birthDate){    public string FirstName { get; } = firstName;    public string LatName { get; } = lastName;    private DateTime _birthDate = birthDate;}

public class Customer( string firstName, string lastName,  DateTime birthDate){    public string FirstName { get; } = firstName;    public string LatName { get; } = lastName;    private DateTime _birthDate = birthDate;}

public class Customer( string firstName, string lastName,  private DateTime birthDate){    public string FirstName { get; } = firstName;    public string LatName { get; } = lastName;}

public class Customer( string firstName, string lastName,  private DateTime birthDate){    public string FirstName { get; } = firstName;    public string LatName { get; } = lastName; public int Age { get { return CalcularAge(birthDate); }  }}

Page 11: C# 6.0 - April 2014 preview

Primary ConstructorsExplicit Constructors

public Point()    : this(0, 0) // calls the primary constructor{}

Page 12: C# 6.0 - April 2014 preview

Primary ConstructorsBase Initializer

class BufferFullException()    : Exception("Buffer full"){}

Page 13: C# 6.0 - April 2014 preview

Primary ConstructorsPartial Types

If a class is declared in multiple parts, only one of those parts can declare a constructor parameters and only that part can declare parameters on the specification of the base class.

Page 14: C# 6.0 - April 2014 preview

Using Static

using System;class Program{    static void Main()    {        Console.WriteLine(Math.Sqrt(3 * 3 + 4 * 4));    }}

using System;class Program{    static void Main()    {        Console.WriteLine(Math.Sqrt(3 * 3 + 4 * 4));    }}

using System.Console;using System.Math;class Program{    static void Main()    {        WriteLine(Sqrt(3 * 3 + 4 * 4));    }}

Page 15: C# 6.0 - April 2014 preview

Using StaticExtension Methods

using System.Linq.Enumerable; // Just the type, not the whole namespaceclass Program{    static void Main()    {        var range = Range(5, 17);        var odd = Where(range, i => i % 2 == 1);        var even = range.Where(i => i % 2 == 0);    }}

using System.Linq.Enumerable; // Just the type, not the whole namespaceclass Program{    static void Main()    {        var range = Range(5, 17);                // Allowed        var odd = Where(range, i => i % 2 == 1); // Not Allowed        var even = range.Where(i => i % 2 == 0); // Allowed    }}

Page 16: C# 6.0 - April 2014 preview

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 … }

from s in stringsselect int.TryParse(s, out int i) ? i : -1;

Page 17: C# 6.0 - April 2014 preview

Excpetion Filters

try{    …}catch (Exception e) if (myfilter(e)){    …}

Page 18: C# 6.0 - April 2014 preview

Binary Literals

var bits = 0b00101110;

Page 19: C# 6.0 - April 2014 preview

Digit Separators

var bits = 0b0010_1110;var hex = 0x00_2E;var dec = 1_234_567_890;

Page 20: C# 6.0 - April 2014 preview

Extension Add Methods in Collection Initializers

On the first implementation of collection initializers in C#, the Add methods that get called couldn’t be extension methods.VB got it right from the start, but it seems C# had been forgoten.This has been fixed: the code generated from a collection initializer will now happily call an extension method called Add.It’s not much of a feature, but it’s occasionally useful, and it turned out implementing it in the new compiler amounted to removing a check that prevented it.

Page 21: C# 6.0 - April 2014 preview

Element Initializers

var numbers = new Dictionary<int, string>{    [7] = “seven",    [9] = “nine",    [13] = “thirteen"};

Page 22: C# 6.0 - April 2014 preview

Indexed Members

var customer = new JsonData{    $first = "Anders",  // => ["first"] = "Anders"    $last = "Hejlsberg" // => ["last"] = "Hejlsberg"};string first = customer.$first; // => customer["first"]

Page 23: C# 6.0 - April 2014 preview

Await in catch and finally blocksResource res = null;try{    // You could do this.    res = await res.OpenAsync(…);           …}catch (ResourceException e){    // Now you can do this …    await res.LogAsync(res, e);              }finally{    // … and this.    if (res != null) await res.CloseAsync();}

Page 24: C# 6.0 - April 2014 preview

Expression-Bodied Members

public class Point(int x, int y){    public int X => x;    public int Y => y;    public double Dist => Math.Sqrt(x* x + y* y);    public Point Move(int dx, int dy) => new Point(x + dx, y + dy);}

Page 25: C# 6.0 - April 2014 preview

Event Initializers

var button = new Button{    ...    Click += (source, e) => ...    ...};

Page 26: C# 6.0 - April 2014 preview

Semicolon Operator

(var x = Foo(); Write(x); x*x)

Page 27: C# 6.0 - April 2014 preview

Params IEnumerable

int Avg(params IEnumerable<int> numbers) { … }

Page 28: C# 6.0 - April 2014 preview

String Interpolation

"\{p.First} \{p.Last} is \{p.Age} years old."

discussion

Page 29: C# 6.0 - April 2014 preview

NameOf Operator

string s = nameof(Console.Write);

Page 30: C# 6.0 - April 2014 preview

Null Propagation

var temp = GetCustomer();string name = (temp == null) ? null : temp.Name;

var temp = GetCustomer()?.Name;

Page 31: C# 6.0 - April 2014 preview

Null PropagationAssociativity

– Left associative• ((a?.b)?.c)

– Right associative• (a?.(b?.c))

int? l = customer?.Nome.Length;

discussion

Page 32: C# 6.0 - April 2014 preview

Protected and Internal

protected internal

protected internal

protectedinternal

discussion

Page 33: C# 6.0 - April 2014 preview

Questions?

Page 34: C# 6.0 - April 2014 preview

demos

Page 35: C# 6.0 - April 2014 preview

Call to action

• Use the IDE and language features• Dip your toes in custom diagnostics• Consider forking the compiler source• Give feedback

Page 36: C# 6.0 - April 2014 preview

ReferencesMSDN

– http://www.msdn.com/roslyn

Download Preview– http://aka.ms/Roslyn

CodePlex– http://roslyn.codeplex.com/

Roslyn source browser– http://source.roslyn.codeplex.com/

Page 37: C# 6.0 - April 2014 preview

ReferencesThe Future of C# - //build/ 2014

– http://channel9.msdn.com/Events/Build/2014/2-577

What’s New for C# Developers - //build/ 2014– http://channel9.msdn.com/Events/Build/2014/9-020

Anders Hejlsberg Live Q&A - //build/ 2014– http://channel9.msdn.com/Events/Build/2014/9-010

The Future of Visual Basic and C# - TechEd North America 2014– http://channel9.msdn.com/Events/TechEd/NorthAmerica/2014/DEV-B336

Channel 9 Live: .NET Framework Q&A - TechEd North America 2014– http://channel9.msdn.com/Events/TechEd/NorthAmerica/2014/C9-22

Page 38: C# 6.0 - April 2014 preview

ReferencesC# – Novas Funcionalidades do C# 6.0 – Antevisão de Abril de 2014

– http://www.revista-programar.info/artigos/c-novas-funcionalidades-do-c-6-0-antevisao-de-abril-de-2014/

A C# 6.0 Language Preview– http://msdn.microsoft.com/en-us/magazine/dn683793.aspx

Page 39: C# 6.0 - April 2014 preview

Questões?

Page 40: C# 6.0 - April 2014 preview

Obrigado!

Paulo Morgadohttp://PauloMorgado.NET/https://twitter.com/PauloMorgadohttps://www.facebook.com/PauloMorgado.NEThttp://www.slideshare.net/PauloJorgeMorgado

Page 41: C# 6.0 - April 2014 preview

Patrocinadores “GOLD”

Twitter: @PTMicrosoft http://www.microsoft.com/portugal

Page 42: C# 6.0 - April 2014 preview

Patrocinadores “Silver”

Page 43: C# 6.0 - April 2014 preview

Patrocinadores “Bronze”

Page 44: C# 6.0 - April 2014 preview

http://bit.ly/netponto-aval-47

* Para quem não puder preencher durante a reunião, iremos enviar um email com o link à tarde

Page 45: C# 6.0 - April 2014 preview

Próximas reuniões presenciais31/05/2014 – Maio (Lisboa)21/06/2014 – Junho (Lisboa)27/07/2014 – Julho (Lisboa)??/??/???? – ????? (Porto)

Reserva estes dias na agenda! :)