c# 6 - do it right vedran kaldi senior
DESCRIPTION
Visual Studio 2015 CTP 5 File -> New -> Project How to use today?TRANSCRIPT
![Page 1: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/1.jpg)
#hashtag@speaker
C# 6 - do it right
Vedran KaldiSenior Developer @Five
![Page 2: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/2.jpg)
No big new conceptsMany small featuresClean up your code
C# v6 design philosophy
![Page 3: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/3.jpg)
Visual Studio 2015 CTP 5File -> New -> Project
How to use today?
![Page 4: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/8.jpg)
Null-conditional operators
OnChanged(this, args);
![Page 9: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/9.jpg)
Null-conditional operators
if (OnChanged != null) { OnChanged(this, args); }
![Page 10: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/10.jpg)
Null-conditional operators{ var onChanged = OnChanged; if (onChanged != null) { onChanged(this, args); }}
![Page 11: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/11.jpg)
Null-conditional operators
OnChanged?.Invoke(this, args);
![Page 12: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/20.jpg)
The nameof operatorpublic Point Add(Point point){ if (point == null) { throw new ArgumentNullException("point"); }}
![Page 21: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/21.jpg)
The nameof operatorpublic Point Add(Point other){ if (other == null) { throw new ArgumentNullException("point"); }}
![Page 22: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/24.jpg)
Exception filterstry{ … }catch (ArgumentException e){ }finally{ }
![Page 25: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/25.jpg)
Exception filterstry{ … }catch (ArgumentException e) if (e.ParamName == nameof(other)){ }finally{ }
![Page 26: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/35.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/38.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/39.jpg)
Await in catch and finallytry{ … }catch (ConfigurationException e) if (e.IsSevere){ await LogAsync(e);}finally{ await CloseAsync();}
![Page 40: C# 6 - do it right Vedran Kaldi Senior](https://reader036.vdocuments.us/reader036/viewer/2022062401/5a4d1b0d7f8b9ab05998c35d/html5/thumbnails/40.jpg)
https://github.com/dotnet/roslynhttps://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14
Learn more at …