Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Csharp7_Talk

 Csharp7_Talk

Mark Allibone

October 23, 2017
Tweet

More Decks by Mark Allibone

Other Decks in Programming

Transcript

  1. 4 C# Entwicklung Managed Code Generics Language Integrated Query Dynamic

    + (Language Parity) C# 5.0 + VB 11.0 Windows 8 Runtime + Asynchrony C# 1.0 + VB 7.0 C# 2.0 + VB 8.0 C# 3.0 + VB 9.0 C# 4.0 + VB 10.0 The Age of Roslyn C# 6.0 + VB 12.0
  2. 6 C# 6 • Initialisierung von Auto Properties • Auto

    Properties ohne Setter • Expression Bodies • Verwendung statischer Klassen • «Null-conditional» Operator • String Interpolation • nameof • Intializer für Collections mit Indexzugriff • Exception Filter • await in catch- und finally-Blöcken
  3. 7 C# 6 • Initialisierung von Auto Properties • Auto

    Properties ohne Setter • Expression Bodies • Verwendung statischer Klassen • «Null-conditional» Operator • String Interpolation • nameof • Intializer für Collections mit Indexzugriff • Exception Filter • await in catch- und finally-Blöcken
  4. 8 C# Entwicklung Managed Code Generics Language Integrated Query Dynamic

    + (Language Parity) C# 5.0 + VB 11.0 Windows 8 Runtime + Asynchrony C# 1.0 + VB 7.0 C# 2.0 + VB 8.0 C# 3.0 + VB 9.0 C# 4.0 + VB 10.0 The Age of Roslyn C# 6.0 + VB 12.0 C# 7
  5. 10

  6. 11 1. Numeric Literals // binary types public const int

    One = 0b0001; public const int Two = 0b0010; // grouping of numbers public const int Sixteen = 0b0001_0000; public const uint HexNumber = 0xFFFF_0000; public long Billion = 1_000_000_000_000L; public const double AvogadroConstant = 6.022_140_857_747_474e23;
  7. 12

  8. 13 2. Tuples var oldStyle = new Tuple<string, string>("a", "b");

    • Install NuGet package System.ValueType var letters = ("a", "b"); letters.Item1 = "firstLetter"; var firstLetters = (Alpha: "a", Beta: "b"); firstLetters.Alpha = "alpha" • Ohne var (string Alpha, string Beta) namedLetters = ("a", "b");
  9. 14 Tuples als Returnwert private static (int Max, int Min)

    Range(IEnumerable<int> numbers) { int min = int.MaxValue; int max = int.MinValue; foreach (var n in numbers) { min = (n < min) ? n : min; max = (n > max) ? n : max; } return (max, min); }
  10. 15 private static (int Max, int Min) Range(IEnumerable<int> numbers) {

    int min = int.MaxValue; int max = int.MinValue; foreach (var n in numbers) { min = (n < min) ? n : min; max = (n > max) ? n : max; } return (max, min); } Tuples als Returnwert
  11. 16

  12. 18 3. Constructor => Destructor => Deconstructor public class Point

    { public double X { get; } public double Y { get; } public Point(double x, double y) { this.X = x; this.Y = y; } public void Deconstruct(out double x, out double y) { x = this.X; y = this.Y; } }
  13. 19 Deconstructor: split into parts public class Point { public

    double X { get; } public double Y { get; } public void Deconstruct(out double x, out double y) { x = this.X; y = this.Y; } } // Deconstruction var p = new Point(3.14, 2.71); (double X, double Y) = p; Console.WriteLine($"(x,y): ({X},{Y})");
  14. 20

  15. 22 int result; // the old style if (int.TryParse(input, out

    result)) { Console.WriteLine(result); } else { Console.WriteLine("Could not parse input"); } 4. Declaration of out Variables
  16. 23 4. Deklaration of out Variables if (int.TryParse(input, out int

    result)) { Console.WriteLine(result); } else { Console.WriteLine("Could not parse input"); }
  17. 24

  18. 26 5. Pattern Matching • 3 Types of Patterns –

    Constant Patterns if (o is null) return; // constant pattern "null" – Type Patterns if (!(o is int i)) return; // type pattern "int i" – Var Patterns • 2 C# language constructs – is – switch
  19. 27 Patterns and Try-Methods object o = 7; if (o

    is int i || (o is string s && int.TryParse(s, out i))) { /* use i */ }
  20. 28 switch Pattern public void PrintShape(object shape) { switch (shape)

    { case Circle c: Console.WriteLine($"circle with radius {c.Radius}"); break; case Rectangle s when (s.Length == s.Height): Console.WriteLine($"{s.Length} x {s.Height} square"); break; case Rectangle r: Console.WriteLine($"{r.Length} x {r.Height} rectangle"); break; default: Console.WriteLine("<unknown shape>"); break; case null: throw new ArgumentNullException(nameof(shape)); } }
  21. 29

  22. 31 6. Local functions // Fibonacci contains a local function

    Fib which cannot be called from the outside. // The local function has access to local variables and parameters of the outer function public int Fibonacci(int x) { if (x < 0) throw new ArgumentException("Less negativity please!", nameof(x)); return Fib(x).current; (int current, int previous) Fib(int i) { if (i == 0) return (1, 0); var (p, pp) = Fib(i - 1); return (p + pp, p); } }
  23. 32 Local functions public IEnumerable<T> Filter<T>(IEnumerable<T> source, Func<T, bool> filter)

    { if (source == null) throw new ArgumentNullException(nameof(source)); if (filter == null) throw new ArgumentNullException(nameof(filter)); return Iterator(); IEnumerable<T> Iterator() { foreach (var element in source) { if (filter(element)) { yield return element; } } } }
  24. 34

  25. 35 static void Main(string[] args) { int[,] matrix = {

    { 1, 2 }, { 11, 12 }, { 21, 22 }, { 31, 32 }, { 41, 42 }, { 51, 52 } }; int item = Find(matrix, (val) => val == 42); // ref var item = ref Find(matrix, (val) => val == 42); Console.WriteLine(item); item = 24; Console.WriteLine(matrix[4, 1]); } public static ref int Find(int[,] matrix, Func<int, bool> predicate) { for (int i = 0; i < matrix.GetLength(0); i++) for (int j = 0; j < matrix.GetLength(1); j++) if (predicate(matrix[i, j])) return ref matrix[i, j]; throw new InvalidOperationException("Not found"); } 7. ref locals and ref returns
  26. 36 static void Main(string[] args) { int[,] matrix = {

    { 1, 2 }, { 11, 12 }, { 21, 22 }, { 31, 32 }, { 41, 42 }, { 51, 52 } }; int item = Find(matrix, (val) => val == 42); // ref var item = ref Find(matrix, (val) => val == 42); Console.WriteLine(item); item = 24; Console.WriteLine(matrix[4, 1]); } public static ref int Find(int[,] matrix, Func<int, bool> predicate) { for (int i = 0; i < matrix.GetLength(0); i++) for (int j = 0; j < matrix.GetLength(1); j++) if (predicate(matrix[i, j])) return ref matrix[i, j]; throw new InvalidOperationException("Not found"); } 7. ref locals and ref returns
  27. 37 static void Main(string[] args) { int[,] matrix = {

    { 1, 2 }, { 11, 12 }, { 21, 22 }, { 31, 32 }, { 41, 42 }, { 51, 52 } }; // int item = Find(matrix, (val) => val == 42); ref var item = ref Find(matrix, (val) => val == 42); Console.WriteLine(item); item = 24; Console.WriteLine(matrix[4, 1]); } public static ref int Find(int[,] matrix, Func<int, bool> predicate) { for (int i = 0; i < matrix.GetLength(0); i++) for (int j = 0; j < matrix.GetLength(1); j++) if (predicate(matrix[i, j])) return ref matrix[i, j]; throw new InvalidOperationException("Not found"); } 7. ref locals and ref returns
  28. 38

  29. 40 8. More expression bodied members public Person(string name) =>

    names.TryAdd(id, name); // constructors ~Person() => names.TryRemove(id, out string s); // destructors public string Name { get => names[id]; // getters set => names[id] = value; // setters } // throw expressions public string GetLastName() => throw new NotImplementedException();
  30. 42

  31. 43

  32. 45 9. Async return Types private bool isIncache = false;

    private int cacheResult; public ValueTask<int> CachedFunc() { return isIncache ? new ValueTask<int>(cacheResult) : new ValueTask<int>(LoadCache()); } private async Task<int> LoadCache() { // simulate async work: await Task.Delay(100); cacheResult = 100; isIncache = true; return cacheResult; }
  33. 46 C# 7 - Takeaways • Numeric Literals • Tuples

    • Deconstructor • Out Parameter • Pattern Matching • ref locals and ref returns (aka Pointer) • Local Functions • More expression bodied members (aka lambdas) • Async return Types
  34. 69 Takeaways • Ensure your maximum productivity with new C#

    features • Give C# 7.1 a try if you want to • Give the team your feedback and suggestions https://github.com/dotnet/csharplang