44
TUGA IT 2016 LISBON, PORTUGAL

Tuga it 2016 - What's New In C# 6

Embed Size (px)

Citation preview

Page 1: Tuga it 2016 - What's New In C# 6

TUGA IT 2016LISBON, PORTUGAL

Page 2: Tuga it 2016 - What's New In C# 6

THANK YOU TO OUR SPONSORS

Page 3: Tuga it 2016 - What's New In C# 6

THANK YOU TO OUR TEAM

ANDRÉ BATISTA ANDRÉ MELANCIA ANDRÉ VALA ANTÓNIO LOURENÇO BRUNO LOPES CLÁUDIO SILVA

NIKO NEUGEBAUER

RUI REISRICARDO CABRAL

NUNO CANCELO PAULO MATOS PEDRO SIMÕES

SANDRA MORGADO SANDRO PEREIRARUI BASTOS

NUNO ÁRIAS SILVA

Page 4: Tuga it 2016 - What's New In C# 6

What’s New In C# 6

Page 5: Tuga it 2016 - What's New In C# 6

Paulo Morgado

Page 6: Tuga it 2016 - What's New In C# 6

Agenda• Improvements in Auto-Properties• Expression Bodied Function Members• The 'using static' Directive • Null-Conditional Operator• String Interpolation• 'nameof' Expressions• 'Add' Extension Methods in Collection Initializers • Index Initializers• Exception Filters• 'await' in 'catch' and 'finally' blocks• C# Interactive

Page 7: Tuga it 2016 - What's New In C# 6

Improvements in Auto-PropertiesInitializers for Auto-Propertiespublic class Person{ private string firstName = "Jane" private string lastName = "Doe";  public string FirstName { get { return firstName; } set { firstName = value; } }  public string LastName { get { return lastName; } set { lastName = value; } }}

Page 8: Tuga it 2016 - What's New In C# 6

Improvements in Auto-PropertiesInitializers for Auto-Propertiespublic class Person{ public string FirstName { get; set; } = "Jane"; public string LastName { get; set; } = "Doe";}

C# 6

Page 9: Tuga it 2016 - What's New In C# 6

Improvements in Auto-PropertiesRead-Only Auto-Propertiespublic class Person{ private readonly string firstName = "Jane"; private readonly string lastName = "Doe";

public string FirstName { get { return firstName; } }

public string LastName { get { return lastName; } }

// ...

public Person(string first, string last) { firstName = first; lastName = last; }}

Page 10: Tuga it 2016 - What's New In C# 6

Improvements in Auto-PropertiesRead-Only Auto-Propertiespublic class Person{ public string FirstName { get; } = "Jane"; public string LastName { get; } = "Doe";

// ...

public Person(string first, string last) { FirstName = first; LastName = last; }} C# 6

Page 11: Tuga it 2016 - What's New In C# 6

Expression Bodied Function MembersExpression Bodied Method-Like Function Memberspublic Point Move(int dx, int dy){ return new Point(x + dx, y + dy);}

public static Complex operator +(Complex a, Complex b){ return a.Add(b);}

public static implicit operator string(Person p){ return p.First + " " + p.Last;}

Page 12: Tuga it 2016 - What's New In C# 6

Expression Bodied Function MembersExpression Bodied Method-Like Function Memberspublic 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;

C# 6

Page 13: Tuga it 2016 - What's New In C# 6

Expression Bodied Function MembersExpression Bodied Property-Like Function Memberspublic string Name{ get { return First + " " + Last; }} public Person this[long id]{ get { return store.LookupPerson(id); }}

Page 14: Tuga it 2016 - What's New In C# 6

Expression Bodied Function MembersExpression Bodied Property-Like Function Memberspublic string Name => First + " " + Last; public Person this[long id] => store.LookupPerson(id);

C# 6

Page 15: Tuga it 2016 - What's New In C# 6

The 'using static' Directive

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

Page 16: Tuga it 2016 - What's New In C# 6

The 'using static' Directive

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

C# 6

Page 17: Tuga it 2016 - What's New In C# 6

The 'using static' DirectiveExtension Methodsusing static System.Linq.Enumerable; // The type, not the namespace class Program{ static void Main() { var range = Range(5, 17); // Ok: not extension var odd = Where(range, i => i % 2 == 1); // Error, not in scope var even = range.Where(i => i % 2 == 0); // Ok }} C# 6

Page 18: Tuga it 2016 - What's New In C# 6

Null-Conditional OperatorProperty and Method Invocationint? nullable = (people != null) ? new int?(people.Length) : null;Person first = (people != null) ? people[0] : null;

int? firstCount = (people != null) ? people[0].Orders.Count() : (int?)null;

int firstCount = (people != null) ? people[0].Orders.Count() : 0;

Page 19: Tuga it 2016 - What's New In C# 6

Null-Conditional OperatorProperty and Method Invocationint? length = people?.Length; // null if people is nullPerson first = people?[0]; // null if people is null int length = people?.Length ?? 0; // 0 if people is null int? firstCount = people?[0].Orders.Count();

C# 6

Page 20: Tuga it 2016 - What's New In C# 6

Null-Conditional OperatorDelegate Invocationvar handler = PropertyChanged;if (handler != null){ handler(this, args);}

Page 21: Tuga it 2016 - What's New In C# 6

Null-Conditional OperatorDelegate InvocationPropertyChanged?.Invoke(this, args);

C# 6

Page 22: Tuga it 2016 - What's New In C# 6

String Interpolation

string.Format("{0} is {1} year{{s}} old.", p.Name, p.Age);

Page 23: Tuga it 2016 - What's New In C# 6

String Interpolation

$"{p.Name} tem {p.Age} ano{{s}}."

$"{p.Name,20} tem {p.Age:D3} ano{{s}}."

$"{p.Name} tem {p.Age} ano{(p.Age == 1 ? "" : "s")}."

C# 6

Page 24: Tuga it 2016 - What's New In C# 6

String InterpolationFormattable StringsIFormattable christmas = $"{new DateTime(2015, 12, 25):f}";var christamasText = christmas.ToString(string.Empty, new CultureInfo("pt-PT"));

C# 6

Page 25: Tuga it 2016 - What's New In C# 6

String InterpolationFormattable StringsFormattableStringFactory.Create("{0:f}", new DateTime(2015, 12, 25)) public abstract class FormattableString : IFormattable{ protected FormattableString(); public abstract int ArgumentCount { get; } public abstract string Format { get; } public static string Invariant(FormattableString formattable); public abstract object GetArgument(int index); public abstract object[] GetArguments(); public override string ToString(); public abstract string ToString(IFormatProvider formatProvider);} C# 6

Page 26: Tuga it 2016 - What's New In C# 6

String InterpolationFormattable StringsFormattableString christmas = $"{new DateTime(2015, 12, 25):f}";var christamasText = christmas.ToString(new CultureInfo("pt-PT"));

C# 6

Page 27: Tuga it 2016 - What's New In C# 6

String InterpolationFormattable Stringspublic static IDbCommand CreateCommand( this IDbConnection connection, FormattableString commandText){ var command = connection.CreateCommand(); command.CommandType = CommandType.Text; if (commandText.ArgumentCount > 0) { var commandTextArguments = new string[commandText.ArgumentCount]; for (var i = 0; i < commandText.ArgumentCount; i++) { commandTextArguments[i] = "@p" + i.ToString(); var p = command.CreateParameter(); p.ParameterName = commandTextArguments[i]; p.Value = commandText.GetArgument(i); command.Parameters.Add(p); } command.CommandText = string.Format(CultureInfo.InvariantCulture, commandText.Format, commandTextArguments); } else { command.CommandText = commandText.Format; } return command;}

var id = 10;var nome = “Paulo";IDbConnection cnn = new SqlConnection();var cmd = cnn.CreateCommand( $"insert into test (id, nome) values({id}, {nome})");cmd.ExecuteNonQuery();

C# 6

Page 28: Tuga it 2016 - What's New In C# 6

'nameof' Expressions

void M1(string x){ if (x == null) throw new ArgumentNullException("x"); var s = "ZipCode";}

Page 29: Tuga it 2016 - What's New In C# 6

'nameof' Expressions

void M1(string x){ if (x == null) throw new ArgumentNullException(nameof(x)); var s = nameof(Person.Address.ZipCode);}

C# 6

Page 30: Tuga it 2016 - What's New In C# 6

'nameof' ExpressionsSource Code vs. Metadatausing S = System.String;void M<T>(S s){ var s1 = nameof(T); var s2 = nameof(S);} using S =

System.String;void M<T>(S s){ var s1 = "T"; var s2 = "S";}

C# 6

Page 31: Tuga it 2016 - What's New In C# 6

Collection Initializers'Add' Extension Methodspublic class C<T> : IEnumerable<T>{ // ...} public static class Cx{ public static void Add<T>(this C<T> c, T i) { // ... }}

var cs = new C<int>();cs.Add(1);cs.Add(2);cs.Add(3);

Page 32: Tuga it 2016 - What's New In C# 6

Collection Initializers'Add' Extension Methodspublic class C<T> : IEnumerable<T>{ // ...} public static class Cx{ public static void Add<T>(this C<T> c, T i) { // ... }} var cs = new C<int> { 1, 2, 3 }; C# 6

Page 33: Tuga it 2016 - What's New In C# 6

Index Initializers

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

Page 34: Tuga it 2016 - What's New In C# 6

Index Initializers

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

C# 6

Page 35: Tuga it 2016 - What's New In C# 6

Exception Filters

try{ //...}catch (SqlException ex) when (ex.Number == 2){ // ...}catch (SqlException ex){ // ...}

Exectued in the context of the throw, not the catch.

C# 6

Page 36: Tuga it 2016 - What's New In C# 6

'await' in 'catch' and 'finally' blocksasync Task M(){ Resource res = null; Exception ex = null; try { res = await Resource.OpenAsync(); } catch (ResourceException e) { ex = e; }  if (ex != null) await Resource.LogAsync(res, e);  if (res != null) await res.CloseAsync();}

Page 37: Tuga it 2016 - What's New In C# 6

'await' in 'catch' and 'finally' blocksasync Task M(){ Resource res = null; try { res = await Resource.OpenAsync(); } catch (ResourceException e) { await Resource.LogAsync(res, e); } finally { if (res != null) await res.CloseAsync(); }} C# 6

Page 38: Tuga it 2016 - What's New In C# 6

C# 6C# Interactive

Page 39: Tuga it 2016 - What's New In C# 6

References

dotnet/roslyn, New Language Features in C# 6• https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6

Page 40: Tuga it 2016 - What's New In C# 6

References

simple talk, What's New in C# 6• https://www.simple-talk.com/dotnet/.net-framework/whats-new-in-c-6/

Page 41: Tuga it 2016 - What's New In C# 6

References

Revista PROGRAMAR, As Novidades Do C# 6• http://www.revista-programar.info/artigos/as-novidades-do-c-sharp-6/

Page 42: Tuga it 2016 - What's New In C# 6

References

C# 6 - Programação com Produtividade • http://www.fca.pt/pt/catalogo/ebooks/informatica/programacao/c-6-programacao-com-pro

dutividade/

Page 43: Tuga it 2016 - What's New In C# 6

Thank You!

Paulo Morgadohttp://PauloMorgado.NET/https://twitter.com/PauloMorgadohttp://netponto.org/membro/paulo-morgado/https://www.facebook.com/paulo.morgadohttps://www.linkedin.com/in/PauloMorgadohttp://www.revista-programar.info/author/pmorgado/https://www.simple-talk.com/author/paulo-morgado/http://www.slideshare.net/PauloJorgeMorgadohttps://docs.com/paulo-morgadohttp://www.fca.pt/pt/catalogo-pesquisa/?filtros=0&pesquisa=paulo+morgado

Page 44: Tuga it 2016 - What's New In C# 6

THANK YOU TO OUR SPONSORS