Upload
cds
View
349
Download
0
Embed Size (px)
Citation preview
C# 6.0
O QUE MUDOU ?
Carlos dos Santos
MVP C#
Code Cracker Project
CDS Informática Ltda.
www.carloscds.net
C# 6.0 OVERVIEW
Auto-property initializers
Parameter-less struct constructors
Using Statements for Static Members
Dictionary Initializer
Await in catch/finally
Exception filters
Expression-bodied members
Null propagation
String interpolation
Nameof operator
AUTO-PROPERTY INITIALIZERS
class Pessoa{
public string Nome { get; set; }}
class Pessoa{
public string Nome { get; } = "Carlos";}
= "Carlos";
AUTO-PROPERTY INITIALIZERS - CONSTRUCTOR
class Pessoa{
public string Nome { get; }
public Pessoa(){
Nome = "Carlos";}
}
PARAMETER-LESS STRUCT CONSTRUCTORS
struct Point{
public int X { get; }public int Y { get; }
}
Read Only!
public Point(){
X = 100;Y = 100;
}
PARAMETER-LESS STRUCT CONSTRUCTORS
Read Only!
struct Point{
public int X { get; }public int Y { get; }
public Point(int x, int y){
X = x;Y = y;
}
public Point() : this(100, 100){}
}
USING STATEMENTS FOR STATIC MEMBERS
class Program{
static void Main(string[] args){
var angle = 90d;Console.WriteLine(Math.Sin(angle));
}}
USING STATEMENTS FOR STATIC MEMBERS
using static System.Console;using static System.Math;
class Program{
static void Main(string[] args){
var angle = 90d;WriteLine(Sin(angle));
}}
DICTIONARY INITIALIZERS
var pessoas = new Dictionary<string, Pessoa>{
["Carlos"] = new Pessoa()};
var cidades = new Dictionary<int, string>{
[1] = "Cornélio Procópio"};
public async Task DownloadAsync(){
}
try{ }catch{
await Task.Delay(2000);}finally{
await Task.Delay(2000);}
AWAIT IN CATCH + FINALLY
EXCEPTION FILTERS
try{
// Faça qualquer coisa}catch (CustomException ex) when (ex.Severity > 50){
Console.WriteLine("ERRO Crítico!!!");}catch (CustomException ex){
Console.WriteLine("Erro");}
NULL PROPAGATION
class Pessoa{
public string Nome { get; set; }public Endereco Endereco { get; set; }
}
Console.WriteLine(p.Endereco.EnderecoLinha1);
var p = new Pessoa{
Nome = "Carlos"};
class Endereco{
public string EnderecoLinha1 { get; set; }public string EnderecoLinha2 { get; set; }
}
Erro!
Console.WriteLine(p.Endereco == null ? "Endereço não existe" : p.Endereco.EnderecoLinha1);
var p = new Pessoa{
Nome = "Carlos"};
NULL PROPAGATION
class Pessoa{
public string Nome { get; set; }public Endereco Endereco { get; set; }
}
class Endereco{
public string EnderecoLinha1 { get; set; }public string EnderecoLinha2 { get; set; }
}
Console.WriteLine(p?.Endereco?.EnderecoLinha1 ?? "Endereço não existe");
NULL PROPAGATION
Console.WriteLine(p.Endereco == null ? "Endereço não existe" : p.Endereco.EnderecoLinha1);
var p = new Pessoa{
Nome = "Carlos"};
class Pessoa{
public string Nome { get; set; }public Endereco Endereco { get; set; }
}
class Endereco{
public string EnderecoLinha1 { get; set; }public string EnderecoLinha2 { get; set; }
}
var pessoas = new[]{
new Pessoa(),null
};
WriteLine(pessoas[0]?.Nome);
WriteLine(pessoas[1]?.Nome);
NULL PROPAGATION
EXPRESSION-BODIED MEMBERS
class Rectangle{
public double Width { get; set; }public double Height { get; set; }
public double Area => Width * Height;}
EXPRESSION-BODIED MEMBERS
class Rectangle{
public double Width { get; set; }public double Height { get; set; }
public override string ToString() => $"Width = {Width} and Height = {Height}";
}
STRING INTERPOLATION
public override string ToString() => "Width = \{Width} and Height = \{Height}";
public override string ToString() => $"Width = {Width} and Height = {Height}";
Syntax mudou para:
STRING INTERPOLATION
public override string ToString() => $"Width = {Width} and Height = {Height}";
public override string ToString(){
object[] args = new object[] { this.Width, this.Height };return string.Format("Width = {0} and Height = {1}", args);
}
NAMEOF OPERATOR
static void Main(string[] args){
WriteLine("Parameter name is: \{nameof(args)}");}
NAMEOF OPERATOR
public double CalculateArea(int width, int height){
if (width <= 0){
throw new ArgumentException($"Parameter {nameof(width)} cannot be less than 0");}return width * height;
}
DEMONSTRAÇÃO
C#6
OBRIGADO!
Carlos dos Santos
CDS Informática Ltda.
@cdssoftware
www.carloscds.net
Microsoft MVP C#