C# 6.0 - April 2014 preview

2,749 views

Published on

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,749
On SlideShare
0
From Embeds
0
Number of Embeds
78
Actions
Shares
0
Downloads
69
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide
  • Para quem puder ir preenchendo, assim não chateio mais logo 
    É importante para recebermos nós feedback, e para darmos feedback aos nossos oradores

    http://goqr.me/
  • C# 6.0 - April 2014 preview

    1. 1. C# 6.0 April 2014 Preview Paulo Morgado http://netponto.org47ª Reunião Lisboa - 31/05/2014
    2. 2. Who Am I? Paulo Morgado CodePlex Revista PROGRAMAR
    3. 3. Agenda • C# Evolution • .NET Compiler Platform (“Roslyn”) • C# 6.0 Features • Demos
    4. 4. C# 1.0 Managed C# 2.0 Generics C# 3.0 LINQ C# 4.0 Dynamic Programming C# 5.0 Asynchronous Programming Windows Runtime C# 6.0 Compiler Platform “Roslyn” C# Evolution
    5. 5. .NET Compiler Platform (“Roslyn”) A reimplementation of C# and VB compilers In C# and VB With rich public APIs On CodePlex
    6. 6. Why “Roslyn”?
    7. 7. C# 6.0 Features Disclaimer: At this point, some of the features are implemente, others are planned and others are being considered.
    8. 8. 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"; }
    9. 9. Primary Constructors Class 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; }
    10. 10. Primary Constructors Field 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); } } }
    11. 11. Primary Constructors Explicit Constructors public Point() : this(0, 0) // calls the primary constructor { }
    12. 12. Primary Constructors Base Initializer class BufferFullException() : Exception("Buffer full") { }
    13. 13. Primary Constructors Partial 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.
    14. 14. 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)); } }
    15. 15. Using Static Extension Methods using System.Linq.Enumerable; // Just the type, not the whole namespace class 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 namespace class 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 } }
    16. 16. 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 strings select int.TryParse(s, out int i) ? i : -1;
    17. 17. Excpetion Filters try { … } catch (Exception e) if (myfilter(e)) { … }
    18. 18. Binary Literals var bits = 0b00101110;
    19. 19. Digit Separators var bits = 0b0010_1110; var hex = 0x00_2E; var dec = 1_234_567_890;
    20. 20. 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.
    21. 21. Element Initializers var numbers = new Dictionary<int, string> { [7] = “seven", [9] = “nine", [13] = “thirteen" };
    22. 22. Indexed Members var customer = new JsonData { $first = "Anders", // => ["first"] = "Anders" $last = "Hejlsberg" // => ["last"] = "Hejlsberg" }; string first = customer.$first; // => customer["first"]
    23. 23. Await in catch and finally blocks Resource 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(); }
    24. 24. 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); }
    25. 25. Event Initializers var button = new Button { ... Click += (source, e) => ... ... };
    26. 26. Semicolon Operator (var x = Foo(); Write(x); x*x)
    27. 27. Params IEnumerable int Avg(params IEnumerable<int> numbers) { … }
    28. 28. String Interpolation "{p.First} {p.Last} is {p.Age} years old." discussion
    29. 29. NameOf Operator string s = nameof(Console.Write);
    30. 30. Null Propagation var temp = GetCustomer(); string name = (temp == null) ? null : temp.Name; var temp = GetCustomer()?.Name;
    31. 31. Null Propagation Associativity – Left associative • ((a?.b)?.c) – Right associative • (a?.(b?.c)) int? l = customer?.Nome.Length; discussion
    32. 32. Protected and Internal protected internal protected internal protectedinternal discussion
    33. 33. Questions?
    34. 34. Call to action • Use the IDE and language features • Dip your toes in custom diagnostics • Consider forking the compiler source • Give feedback
    35. 35. References MSDN – http://www.msdn.com/roslyn Download Preview – http://aka.ms/Roslyn CodePlex – http://roslyn.codeplex.com/ Roslyn source browser – http://source.roslyn.codeplex.com/
    36. 36. References The 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
    37. 37. References C# – 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
    38. 38. Questões?
    39. 39. Obrigado! Paulo Morgado http://PauloMorgado.NET/ https://twitter.com/PauloMorgado https://www.facebook.com/PauloMorgado.NET http://www.slideshare.net/PauloJorgeMorgado
    40. 40. Patrocinadores “GOLD” Twitter: @PTMicrosoft http://www.microsoft.com/portugal
    41. 41. Patrocinadores “Silver”
    42. 42. Patrocinadores “Bronze”
    43. 43. http://bit.ly/netponto-aval-47 * Para quem não puder preencher durante a reunião, iremos enviar um email com o link à tarde
    44. 44. Próximas reuniões presenciais 31/05/2014 – Maio (Lisboa) 21/06/2014 – Junho (Lisboa) 27/07/2014 – Julho (Lisboa) ??/??/???? – ????? (Porto) Reserva estes dias na agenda! :)

    ×