Presentation is loading. Please wait.

Presentation is loading. Please wait.

New Features in C# 7.0 Mads Torgersen C# Program Manager

Similar presentations


Presentation on theme: "New Features in C# 7.0 Mads Torgersen C# Program Manager"— Presentation transcript:

1 New Features in C# 7.0 Mads Torgersen C# Program Manager

2 Binary literals int[] numbers = { 1, 2, 4, 8, 16, 32 };
9/19/2018 7:11 PM Binary literals int[] numbers = { 1, 2, 4, 8, 16, 32 }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

3 Binary literals int[] numbers = { 0b1, 2, 4, 8, 16, 32 };
9/19/2018 7:11 PM Binary literals int[] numbers = { 0b1, 2, 4, 8, 16, 32 }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

4 Binary literals int[] numbers = { 0b1, 0b10, 4, 8, 16, 32 };
9/19/2018 7:11 PM Binary literals int[] numbers = { 0b1, 0b10, 4, 8, 16, 32 }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

5 Binary literals int[] numbers = { 0b1, 0b10, 0b100, 8, 16, 32 };
9/19/2018 7:11 PM Binary literals int[] numbers = { 0b1, 0b10, 0b100, 8, 16, 32 }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

6 Binary literals int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 16, 32 };
9/19/2018 7:11 PM Binary literals int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 16, 32 }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

7 Binary literals int[] numbers =
9/19/2018 7:11 PM Binary literals int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b10000, 32 }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

8 Digit separators int[] numbers =
9/19/2018 7:11 PM Digit separators int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 32 }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

9 Digit separators int[] numbers =
9/19/2018 7:11 PM Digit separators int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 3_2 }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

10 Digit separators int[] numbers =
9/19/2018 7:11 PM Digit separators int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 3_______2 }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

11 Digit separators int[] numbers =
9/19/2018 7:11 PM Digit separators int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

12 Digit separators int[] numbers =
9/19/2018 7:11 PM Digit separators int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

13 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

14 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var t = Tally(numbers); © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

15 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var t = Tally(numbers); (int, int) Tally(int[] values) { } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

16 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var t = Tally(numbers); (int, int) Tally(int[] values) { var r = (0, 0); return r; } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

17 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var t = Tally(numbers); WriteLine($"Sum: {t.Item1}, count: {t.Item2}"); (int, int) Tally(int[] values) { var r = (0, 0); return r; } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

18 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var t = Tally(numbers); WriteLine($"Sum: {t.Item1}, count: {t.Item2}"); (int sum, int count) Tally(int[] values) { var r = (0, 0); return r; } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

19 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var t = Tally(numbers); WriteLine($"Sum: {t.sum}, count: {t.count}"); (int sum, int count) Tally(int[] values) { var r = (0, 0); return r; } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

20 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var t = Tally(numbers); WriteLine($"Sum: {t.sum}, count: {t.count}"); (int sum, int count) Tally(int[] values) { var r = (s:0, c:0); return r; } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

21 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var (sum, count) = Tally(numbers); WriteLine($"Sum: {t.sum}, count: {t.count}"); (int sum, int count) Tally(int[] values) { var r = (s:0, c:0); return r; } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

22 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var (sum, count) = Tally(numbers); WriteLine($"Sum: {sum}, count: {count}"); (int sum, int count) Tally(int[] values) { var r = (s:0, c:0); return r; } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

23 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var (sum, count) = Tally(numbers); WriteLine($"Sum: {sum}, count: {count}"); (int sum, int count) Tally(int[] values) { var r = (s:0, c:0); return r; } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

24 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var (sum, count) = Tally(numbers); WriteLine($"Sum: {sum}, count: {count}"); (int sum, int count) Tally(int[] values) { var r = (s:0, c:0); foreach (var v in values) } return r; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

25 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var (sum, count) = Tally(numbers); WriteLine($"Sum: {sum}, count: {count}"); (int sum, int count) Tally(int[] values) { var r = (s:0, c:0); foreach (var v in values) r = (r.s + v, r.c + 1); } return r; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

26 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var (sum, count) = Tally(numbers); WriteLine($"Sum: {sum}, count: {count}"); (int sum, int count) Tally(int[] values) { var r = (s:0, c:0); foreach (var v in values) r.s += v; } return r; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

27 9/19/2018 7:11 PM Tuples int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var (sum, count) = Tally(numbers); WriteLine($"Sum: {sum}, count: {count}"); (int sum, int count) Tally(int[] values) { var r = (s:0, c:0); foreach (var v in values) r.s += v; r.c++; } return r; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

28 Local functions int[] numbers =
9/19/2018 7:11 PM Local functions int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var (sum, count) = Tally(numbers); WriteLine($"Sum: {sum}, count: {count}"); (int sum, int count) Tally(int[] values) { var r = (s:0, c:0); foreach (var v in values) r.s += v; r.c++; } return r; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

29 Local functions int[] numbers =
9/19/2018 7:11 PM Local functions int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var (sum, count) = Tally(numbers); WriteLine($"Sum: {sum}, count: {count}"); (int sum, int count) Tally(int[] values) { var r = (s:0, c:0); foreach (var v in values) Add(v, 1); } return r; © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

30 Local functions int[] numbers =
9/19/2018 7:11 PM Local functions int[] numbers = { 0b1, 0b10, 0b100, 0b1000, 0b1_0000, 0b10_0000 }; var (sum, count) = Tally(numbers); WriteLine($"Sum: {sum}, count: {count}"); (int sum, int count) Tally(int[] values) { var r = (s:0, c:0); foreach (var v in values) Add(v, 1); } return r; void Add(int s, int c) { r.s += s; r.c += c; } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

31 Pattern matching public void PrintStars(object o) { }
9/19/2018 7:11 PM Pattern matching public void PrintStars(object o) { } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

32 Pattern matching public void PrintStars(object o) {
9/19/2018 7:11 PM Pattern matching public void PrintStars(object o) { if (o is null) return; // constant pattern "null" } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

33 Pattern matching public void PrintStars(object o) {
9/19/2018 7:11 PM Pattern matching public void PrintStars(object o) { if (o is null) return; // constant pattern "null" if (!(o is int i)) return; // type pattern "int i" } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

34 Pattern matching public void PrintStars(object o) {
9/19/2018 7:11 PM Pattern matching public void PrintStars(object o) { if (o is null) return; // constant pattern "null" if (!(o is int i)) return; // type pattern "int i" WriteLine(new string('*', i)); } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

35 Pattern matching switch (shape) // Switch on anything { }
9/19/2018 7:11 PM Pattern matching switch (shape) // Switch on anything { } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

36 Pattern matching switch (shape) // Switch on anything { default:
9/19/2018 7:11 PM Pattern matching switch (shape) // Switch on anything { default: WriteLine("<unknown shape>"); break; } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

37 Pattern matching switch (shape) // Switch on anything { case null:
9/19/2018 7:11 PM Pattern matching switch (shape) // Switch on anything { case null: throw new ArgumentNullException(nameof(shape)); default: WriteLine("<unknown shape>"); break; } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

38 Pattern matching switch (shape) // Switch on anything {
9/19/2018 7:11 PM Pattern matching switch (shape) // Switch on anything { case Circle c: // Type pattern break; case null: throw new ArgumentNullException(nameof(shape)); default: WriteLine("<unknown shape>"); } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

39 Pattern matching switch (shape) // Switch on anything {
9/19/2018 7:11 PM Pattern matching switch (shape) // Switch on anything { case Circle c: // Type pattern WriteLine($"circle with radius {c.Radius}"); // use c break; case null: throw new ArgumentNullException(nameof(shape)); default: WriteLine("<unknown shape>"); } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

40 Pattern matching switch (shape) // Switch on anything {
9/19/2018 7:11 PM Pattern matching switch (shape) // Switch on anything { case Rectangle r: WriteLine($"{r.Length} x {r.Height} rectangle"); break; case Circle c: // Type pattern WriteLine($"circle with radius {c.Radius}"); // use c case null: throw new ArgumentNullException(nameof(shape)); default: WriteLine("<unknown shape>"); } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

41 Pattern matching switch (shape) // Switch on anything {
9/19/2018 7:11 PM Pattern matching switch (shape) // Switch on anything { case Rectangle s when (s.Length == s.Height): // when-condition WriteLine($"{s.Length} x {s.Height} square"); break; case Rectangle r: WriteLine($"{r.Length} x {r.Height} rectangle"); case Circle c: // Type pattern WriteLine($"circle with radius {c.Radius}"); // use c case null: throw new ArgumentNullException(nameof(shape)); default: WriteLine("<unknown shape>"); } © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

42 What you’ve seen Binary literals and digit separators Tuples
Local functions Pattern matching More at: blogs.msdn.microsoft.com/dotnet/ /08/24/whats-new-in-csharp-7-0/


Download ppt "New Features in C# 7.0 Mads Torgersen C# Program Manager"

Similar presentations


Ads by Google