Click here to load reader
Upload
paulo-morgado
View
2.691
Download
0
Embed Size (px)
Citation preview
C# 6.0 April 2014 Preview Paulo Morgado
http://netponto.org47ª Reunião Lisboa - 31/05/2014
Who Am I?
Paulo Morgado
CodePlex
Revista PROGRAMAR
Agenda• C# Evolution• .NET Compiler Platform (“Roslyn”)• C# 6.0 Features• Demos
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
.NET Compiler Platform (“Roslyn”)
A reimplementation of C# and VB compilersIn C# and VBWith rich public APIsOn CodePlex
Why “Roslyn”?
TeamClean architecture to evolve on
PartnersSource-based tools and extensions
DevelopersRicher C# IDE experience
OSS
1,000,000s
1,000s
10s
C# 6.0 Features
Disclaimer:At this point, some of the features are implemente, others are planned and others are being considered.
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";}
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;}
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); } }}
Primary ConstructorsExplicit Constructors
public Point() : this(0, 0) // calls the primary constructor{}
Primary ConstructorsBase Initializer
class BufferFullException() : Exception("Buffer full"){}
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.
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)); }}
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 }}
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;
Excpetion Filters
try{ …}catch (Exception e) if (myfilter(e)){ …}
Binary Literals
var bits = 0b00101110;
Digit Separators
var bits = 0b0010_1110;var hex = 0x00_2E;var dec = 1_234_567_890;
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.
Element Initializers
var numbers = new Dictionary<int, string>{ [7] = “seven", [9] = “nine", [13] = “thirteen"};
Indexed Members
var customer = new JsonData{ $first = "Anders", // => ["first"] = "Anders" $last = "Hejlsberg" // => ["last"] = "Hejlsberg"};string first = customer.$first; // => customer["first"]
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();}
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);}
Event Initializers
var button = new Button{ ... Click += (source, e) => ... ...};
Semicolon Operator
(var x = Foo(); Write(x); x*x)
Params IEnumerable
int Avg(params IEnumerable<int> numbers) { … }
String Interpolation
"\{p.First} \{p.Last} is \{p.Age} years old."
discussion
NameOf Operator
string s = nameof(Console.Write);
Null Propagation
var temp = GetCustomer();string name = (temp == null) ? null : temp.Name;
var temp = GetCustomer()?.Name;
Null PropagationAssociativity
– Left associative• ((a?.b)?.c)
– Right associative• (a?.(b?.c))
int? l = customer?.Nome.Length;
discussion
Protected and Internal
protected internal
protected internal
protectedinternal
discussion
Questions?
demos
Call to action
• Use the IDE and language features• Dip your toes in custom diagnostics• Consider forking the compiler source• Give feedback
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/
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
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
Questões?
Obrigado!
Paulo Morgadohttp://PauloMorgado.NET/https://twitter.com/PauloMorgadohttps://www.facebook.com/PauloMorgado.NEThttp://www.slideshare.net/PauloJorgeMorgado
Patrocinadores “GOLD”
Twitter: @PTMicrosoft http://www.microsoft.com/portugal
Patrocinadores “Silver”
Patrocinadores “Bronze”
http://bit.ly/netponto-aval-47
* Para quem não puder preencher durante a reunião, iremos enviar um email com o link à tarde
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! :)