c# 6 - do it right vedran kaldi senior

40
#hashtag @speaker C# 6 - do it right Vedran Kaldi Senior Developer @Five

Upload: derick-patrick

Post on 17-Jan-2018

226 views

Category:

Documents


0 download

DESCRIPTION

Visual Studio 2015 CTP 5 File -> New -> Project How to use today?

TRANSCRIPT

Page 1: C# 6 - do it right Vedran Kaldi Senior

#hashtag@speaker

C# 6 - do it right

Vedran KaldiSenior Developer @Five

Page 2: C# 6 - do it right Vedran Kaldi Senior

No big new conceptsMany small featuresClean up your code

C# v6 design philosophy

Page 3: C# 6 - do it right Vedran Kaldi Senior

Visual Studio 2015 CTP 5File -> New -> Project

How to use today?

Page 4: C# 6 - do it right Vedran Kaldi Senior

Null-conditional operatorspublic static Point FromJson(JObject json){ if (json != null && json["x"] != null && json["x"].Type == JTokenType.Integer && json["y"] != null && json["y"].Type == JTokenType.Integer) { return new Point((int)json["x"], (int)json["y"]); } 

return null;}

Page 5: C# 6 - do it right Vedran Kaldi Senior

Null-conditional operatorspublic static Point FromJson(JObject json){ if (json != null && json["x"]?.Type == JTokenType.Integer && json["y"]?.Type == JTokenType.Integer) { return new Point((int)json["x"], (int)json["y"]); } 

return null;}

?.

Page 6: C# 6 - do it right Vedran Kaldi Senior

Null-conditional operatorspublic static Point FromJson(JObject json){ if (json != null && json["x"]?.Type == JTokenType.Integer && json["y"]?.Type == JTokenType.Integer) { return new Point((int)json["x"], (int)json["y"]); } 

return null;}

Page 7: C# 6 - do it right Vedran Kaldi Senior

Null-conditional operatorspublic static Point FromJson(JObject json){ if (json?["x"]?.Type == JTokenType.Integer && json?["y"]?.Type == JTokenType.Integer) { return new Point((int)json["x"], (int)json["y"]); } 

return null;}

Page 8: C# 6 - do it right Vedran Kaldi Senior

Null-conditional operators

OnChanged(this, args);

Page 9: C# 6 - do it right Vedran Kaldi Senior

Null-conditional operators

if (OnChanged != null) { OnChanged(this, args); }

Page 10: C# 6 - do it right Vedran Kaldi Senior

Null-conditional operators{ var onChanged = OnChanged; if (onChanged != null) { onChanged(this, args); }}

Page 11: C# 6 - do it right Vedran Kaldi Senior

Null-conditional operators

OnChanged?.Invoke(this, args);

Page 12: C# 6 - do it right Vedran Kaldi Senior

String interpolationusing System.Math;

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

public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Sqrt(X * X + Y * Y); } }

public override string ToString() { return String.Format("({0}, {1})", X, Y); }}

Page 13: C# 6 - do it right Vedran Kaldi Senior

String interpolationusing System.Math;

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

public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Sqrt(X * X + Y * Y); } }

public override string ToString() { return $"({X}, {Y})"; }}

Page 14: C# 6 - do it right Vedran Kaldi Senior

Expression-bodied methodsusing System.Math;

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

public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Sqrt(X * X + Y * Y); } }

public override string ToString() { return $"({X}, {Y})"; }}

Page 15: C# 6 - do it right Vedran Kaldi Senior

Expression-bodied methodsusing System.Math;

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

public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Sqrt(X * X + Y * Y); } }

public override string ToString() { return $"({X}, {Y})"; }}

() => { return $"({X}, {Y})"; }

() => $"({X}, {Y})"

Page 16: C# 6 - do it right Vedran Kaldi Senior

Expression-bodied methodsusing System.Math;

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

public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Sqrt(X * X + Y * Y); } }

public override string ToString() => $"({X}, {Y})";}

Page 17: C# 6 - do it right Vedran Kaldi Senior

Expression-bodied propertiesusing static System.Math;

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

public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Sqrt(X * X + Y * Y); } } public override string ToString() => $"({X}, {Y})";}

Page 18: C# 6 - do it right Vedran Kaldi Senior

Expression-bodied propertiesusing static System.Math;

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

public Point(int x, int y) { X = x; Y = y; }

public double Dist => Sqrt(X * X + Y * Y); public override string ToString() => $"({X}, {Y})";}

Page 19: C# 6 - do it right Vedran Kaldi Senior

Expression-bodied propertiesusing static System.Math;

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

public Point(int x, int y) { X = x; Y = y; }

public double Dist => Sqrt(X * X + Y * Y); public override string ToString() => $"({X}, {Y})";}

Page 20: C# 6 - do it right Vedran Kaldi Senior

The nameof operatorpublic Point Add(Point point){ if (point == null) { throw new ArgumentNullException("point"); }}

Page 21: C# 6 - do it right Vedran Kaldi Senior

The nameof operatorpublic Point Add(Point other){ if (other == null) { throw new ArgumentNullException("point"); }}

Page 22: C# 6 - do it right Vedran Kaldi Senior

The nameof operatorpublic Point Add(Point point){ if (point == null) { throw new ArgumentNullException(nameof(point)); }}

Page 23: C# 6 - do it right Vedran Kaldi Senior

The nameof operatorpublic Point Add(Point other){ if (other == null) { throw new ArgumentNullException(nameof(other)); }}

Page 24: C# 6 - do it right Vedran Kaldi Senior

Exception filterstry{ … }catch (ArgumentException e){ }finally{ }

Page 25: C# 6 - do it right Vedran Kaldi Senior

Exception filterstry{ … }catch (ArgumentException e) if (e.ParamName == nameof(other)){ }finally{ }

Page 26: C# 6 - do it right Vedran Kaldi Senior

Exception filterstry{ … }catch (ArgumentException e) if (e.ParamName == nameof(other)){}catch (ArgumentException e) if (e.ParamName == nameof(param2)){}finally{ }

Page 27: C# 6 - do it right Vedran Kaldi Senior

Getter-only auto-propertiespublic class Point{ public int X { get; set; } public int Y { get; set; }  public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Math.Sqrt(X * X + Y * Y); } }

public override string ToString() { return String.Format("({0}, {1})", X, Y); }}

Page 28: C# 6 - do it right Vedran Kaldi Senior

Getter-only auto-propertiespublic class Point{ public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Math.Sqrt(X * X + Y * Y); } }

public override string ToString() { return String.Format("({0}, {1})", X, Y); }}

Page 29: C# 6 - do it right Vedran Kaldi Senior

Getter-only auto-propertiespublic class Point{ public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; // samo u konstruktoru!!! } public double Dist { get { return Math.Sqrt(X * X + Y * Y); } }

public override string ToString() { return String.Format("({0}, {1})", X, Y); }}

Page 30: C# 6 - do it right Vedran Kaldi Senior

Getter-only auto-propertiespublic class Point{ public int X { get; } // Pitanje: kako ovo trenutno riješavamo? public int Y { get; } public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Math.Sqrt(X * X + Y * Y); } }

public override string ToString() { return String.Format("({0}, {1})", X, Y); }}

Page 31: C# 6 - do it right Vedran Kaldi Senior

Getter-only auto-propertiespublic class Point{ public int X { get; private set; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Math.Sqrt(X * X + Y * Y); } }

public override string ToString() { return String.Format("({0}, {1})", X, Y); }}

Page 32: C# 6 - do it right Vedran Kaldi Senior

Initializers for auto-propertiespublic class Point{ public int X { get; } = 5; public int Y { get; } = 7; public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Math.Sqrt(X * X + Y * Y); } }

public override string ToString() { return String.Format("({0}, {1})", X, Y); }}

Page 33: C# 6 - do it right Vedran Kaldi Senior

Using static classes

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

public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Math.Sqrt(X * X + Y * Y); } }

public override string ToString() { return String.Format("({0}, {1})", X, Y); }}

Page 34: C# 6 - do it right Vedran Kaldi Senior

Using static classesusing static System.Math;

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

public Point(int x, int y) { X = x; Y = y; }

public double Dist { get { return Sqrt(X * X + Y * Y); } }

public override string ToString() { return String.Format("({0}, {1})", X, Y); }}

Page 35: C# 6 - do it right Vedran Kaldi Senior

Index initializerspublic class Point{ public int X { get; } public int Y { get; }

public Dictionary<string, int> ToJson() { var result = new Dictionary<string, int>(); result["x"] = X; result["y"] = Y; return result; }}

Page 36: C# 6 - do it right Vedran Kaldi Senior

Index initializerspublic class Point{ public int X { get; } public int Y { get; }

public Dictionary<string, int> ToJson() { var result = new Dictionary<string, int>() { ["x"] = X, ["y"] = Y }; return result; }}

Page 37: C# 6 - do it right Vedran Kaldi Senior

Index initializerspublic class Point{ public int X { get; } public int Y { get; }

public Dictionary<string, int> ToJson() { return new Dictionary<string, int>() { ["x"] = X, ["y"] = Y }; }}

Page 38: C# 6 - do it right Vedran Kaldi Senior

Index initializerspublic class Point{ public int X { get; } public int Y { get; }

public Dictionary<string, int> ToJson() => new Dictionary<string, int>() { ["x"] = X, ["y"] = Y };}

Page 39: C# 6 - do it right Vedran Kaldi Senior

Await in catch and finallytry{ … }catch (ConfigurationException e) if (e.IsSevere){ await LogAsync(e);}finally{ await CloseAsync();}