Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mads Torgersen Language PM for C# What’s new in C# 6.0.

Similar presentations


Presentation on theme: "Mads Torgersen Language PM for C# What’s new in C# 6.0."— Presentation transcript:

1 Mads Torgersen Language PM for C# What’s new in C# 6.0

2 No big new concepts Many small features Clean up your code Design philosophy

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

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

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

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

7 Using static classes 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 Math.Sqrt(X * X + Y * Y); } } public override string ToString() { return String.Format("({0}, {1})", X, Y); }

8 Using static classes 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 Math.Sqrt(X * X + Y * Y); } } public override string ToString() { return String.Format("({0}, {1})", X, Y); }

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

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

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

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

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

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() => "(\{X}, \{Y})"; }

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

16 Expression-bodied properties 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 => Sqrt(X * X + Y * Y); public override string ToString() => "(\{X}, \{Y})"; }

17 Expression-bodied properties 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 => Sqrt(X * X + Y * Y); public override string ToString() => "(\{X}, \{Y})"; }

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

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

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

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

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

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

24 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; } ?.?.

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

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

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

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

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

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

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

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

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

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

35 Exception filters try { … } catch (ConfigurationException e) { } finally { }

36 Exception filters try { … } catch (ConfigurationException e) if (e.IsSevere) { } finally { }

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

38 roslyn.codeplex.com Learn more at …


Download ppt "Mads Torgersen Language PM for C# What’s new in C# 6.0."

Similar presentations


Ads by Google