3. 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
4. Improvements in Auto-Properties
Initializers for Auto-Properties
public 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; }
}
}
5. Improvements in Auto-Properties
Initializers for Auto-Properties
public class Person
{
public string FirstName { get; set; } = "Jane";
public string LastName { get; set; } = "Doe";
}
C# 6
6. Improvements in Auto-Properties
Read-Only Auto-Properties
public 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;
}
}
7. Improvements in Auto-Properties
Read-Only Auto-Properties
public class Person
{
public string FirstName { get; } = "Jane";
public string LastName { get; } = "Doe";
// ...
public Person(string first, string last)
{
FirstName = first;
LastName = last;
}
}
C# 6
8. Expression Bodied Function Members
Expression Bodied Method-Like Function Members
public 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;
}
9. Expression Bodied Function Members
Expression Bodied Method-Like 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;
C# 6
10. Expression Bodied Function Members
Expression Bodied Property-Like Function Members
public string Name
{
get { return First + " " + Last; }
}
public Person this[long id]
{
get { return store.LookupPerson(id); }
}
11. Expression Bodied Function Members
Expression Bodied Property-Like Function Members
public string Name => First + " " + Last;
public Person this[long id] => store.LookupPerson(id);
C# 6
12. 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);
}
}
13. 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
14. The 'using static' Directive
Extension Methods
using 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
15. Null-Conditional Operator
Property and Method Invocation
int? nullable = (people != null) ? new int?(people.Length) : null;
Person person = (people != null) ? people[0] : null;
int? first = (people != null) ? people[0].Orders.Count() : (int?)null;
16. Null-Conditional Operator
Property and Method Invocation
int? length = people?.Length; // null if people is null
Person first = people?[0]; // null if people is null
int length = people?.Length ?? 0; // 0 if people is null
int? first = people?[0].Orders.Count();
C# 6
22. String Interpolation
Formattable Strings
FormattableStringFactory.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);
}
.NET 4.6 or highier
C# 6
26. 'nameof' Expressions
void M1(string x)
{
if (x == null) throw new ArgumentNullException(nameof(x));
var s = nameof(Person.Address.ZipCode);
}
C# 6
27. 'nameof' Expressions
Source Code vs. Metadata
using 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
28. Collection Initializers
'Add' Extension Methods
public 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);
29. Collection Initializers
'Add' Extension Methods
public 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
30. Index Initializers
var numbers = new Dictionary<int, string>();
numbers[7] = "sete";
numbers[9] = "nove";
numbers[13] = "treze";
37. Referências
dotnet/roslyn, New Language Features in C# 6
– https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6
simple talk, What's New in C# 6
– https://www.simple-talk.com/dotnet/.net-framework/whats-new-in-c-6/
Revista PROGRAMAR, As Novidades Do C# 6
– http://www.revista-programar.info/artigos/as-novidades-do-c-sharp-6/
39. Próximas reuniões presenciais
23/01/2016 – Janeiro – Lisboa
20/02/2016 – Fevereiro – Braga
27/02/2016 – Fevereiro – Lisboa
19/03/2016 – Março – Lisboa
26/03/2016 – Março – Porto
Reserva estes dias na agenda! :)
Survs ajuda com os inqueritos e Nucleo de Estudantes de Informática do ISEP que nos ajudaram e apoiaram na divulgação e na organização
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/