23
C# 6.0 O QUE MUDOU ? Carlos dos Santos MVP C# Code Cracker Project CDS Informática Ltda. www.carloscds.net

C# 6

  • Upload
    cds

  • View
    349

  • Download
    0

Embed Size (px)

Citation preview

Page 1: C# 6

C# 6.0

O QUE MUDOU ?

Carlos dos Santos

MVP C#

Code Cracker Project

CDS Informática Ltda.

www.carloscds.net

Page 2: C# 6

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

Page 3: C# 6

AUTO-PROPERTY INITIALIZERS

class Pessoa{

public string Nome { get; set; }}

class Pessoa{

public string Nome { get; } = "Carlos";}

= "Carlos";

Page 4: C# 6

AUTO-PROPERTY INITIALIZERS - CONSTRUCTOR

class Pessoa{

public string Nome { get; }

public Pessoa(){

Nome = "Carlos";}

}

Page 5: C# 6

PARAMETER-LESS STRUCT CONSTRUCTORS

struct Point{

public int X { get; }public int Y { get; }

}

Read Only!

public Point(){

X = 100;Y = 100;

}

Page 6: C# 6

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){}

}

Page 7: C# 6

USING STATEMENTS FOR STATIC MEMBERS

class Program{

static void Main(string[] args){

var angle = 90d;Console.WriteLine(Math.Sin(angle));

}}

Page 8: C# 6

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));

}}

Page 9: C# 6

DICTIONARY INITIALIZERS

var pessoas = new Dictionary<string, Pessoa>{

["Carlos"] = new Pessoa()};

var cidades = new Dictionary<int, string>{

[1] = "Cornélio Procópio"};

Page 10: C# 6

public async Task DownloadAsync(){

}

try{ }catch{

await Task.Delay(2000);}finally{

await Task.Delay(2000);}

AWAIT IN CATCH + FINALLY

Page 11: C# 6

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");}

Page 12: C# 6

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!

Page 13: C# 6

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; }

}

Page 14: C# 6

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; }

}

Page 15: C# 6

var pessoas = new[]{

new Pessoa(),null

};

WriteLine(pessoas[0]?.Nome);

WriteLine(pessoas[1]?.Nome);

NULL PROPAGATION

Page 16: C# 6

EXPRESSION-BODIED MEMBERS

class Rectangle{

public double Width { get; set; }public double Height { get; set; }

public double Area => Width * Height;}

Page 17: C# 6

EXPRESSION-BODIED MEMBERS

class Rectangle{

public double Width { get; set; }public double Height { get; set; }

public override string ToString() => $"Width = {Width} and Height = {Height}";

}

Page 18: C# 6

STRING INTERPOLATION

public override string ToString() => "Width = \{Width} and Height = \{Height}";

public override string ToString() => $"Width = {Width} and Height = {Height}";

Syntax mudou para:

Page 19: C# 6

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);

}

Page 20: C# 6

NAMEOF OPERATOR

static void Main(string[] args){

WriteLine("Parameter name is: \{nameof(args)}");}

Page 21: C# 6

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;

}

Page 22: C# 6

DEMONSTRAÇÃO

C#6

Page 23: C# 6

OBRIGADO!

Carlos dos Santos

CDS Informática Ltda.

@cdssoftware

www.carloscds.net

Microsoft MVP C#