Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# 6 - do it right Vedran Kaldi Senior

Similar presentations


Presentation on theme: "C# 6 - do it right Vedran Kaldi Senior"— Presentation transcript:

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

2 No big new concepts Many small features Clean up your code C# v6 design philosophy

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

4 Null-conditional operators public 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; }

5 Null-conditional operators public 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; } ?.?.

6 Null-conditional operators public 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; }

7 Null-conditional operators public 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; }

8 Null-conditional operators OnChanged(this, args);

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

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

11 Null-conditional operators OnChanged?.Invoke(this, args);

12 String interpolation using 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); }

13 String interpolation using 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})"; }

14 Expression-bodied methods using 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})"; }

15 Expression-bodied methods using 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})"

16 Expression-bodied methods using 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})"; }

17 Expression-bodied properties using 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})"; }

18 Expression-bodied properties using 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})"; }

19 Expression-bodied properties using 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})"; }

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

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

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

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

24 Exception filters try { … } catch (ArgumentException e) { } finally { }

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

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

27 Getter-only auto-properties public 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); }

28 Getter-only auto-properties 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); }

29 Getter-only auto-properties public 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); }

30 Getter-only auto-properties public 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); }

31 Getter-only auto-properties public 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); }

32 Initializers for auto-properties public 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); }

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

34 Using static classes using 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); }

35 Index initializers public class Point { public int X { get; } public int Y { get; } … public Dictionary ToJson() { var result = new Dictionary (); result["x"] = X; result["y"] = Y; return result; }

36 Index initializers public class Point { public int X { get; } public int Y { get; } … public Dictionary ToJson() { var result = new Dictionary () { ["x"] = X, ["y"] = Y }; return result; }

37 Index initializers public class Point { public int X { get; } public int Y { get; } … public Dictionary ToJson() { return new Dictionary () { ["x"] = X, ["y"] = Y }; }

38 Index initializers public class Point { public int X { get; } public int Y { get; } … public Dictionary ToJson() => new Dictionary () { ["x"] = X, ["y"] = Y }; }

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

40 https://github.com/dotnet/roslyn https://github.com/dotnet/roslyn/wiki/Languages-features- in-C%23-6-and-VB-14 Learn more at …


Download ppt "C# 6 - do it right Vedran Kaldi Senior"

Similar presentations


Ads by Google