Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

C# - What's Next?

93 views

Published on

C# - what's new with C# 7, what's planned with C# 8: productivity, performance, avoiding errors with tuples, pattern matching, nullable reference types, and more

Published in: Software
  • Be the first to comment

  • Be the first to like this

C# - What's Next?

  1. 1. C# - What's Next? Christian Nagel csharp.christiannagel.com
  2. 2. Goals • Wie entwickelt sich C#? • Versionen • C# Current (C# 7.x) • Future Plans • Constraints • Einige Features können jetzt schon verwendet werden • Etwas wird sich noch ändern
  3. 3. Agenda Productivity Performance Reduce Errors
  4. 4. Christian Nagel • Training • Coaching • Consulting • Microsoft MVP • www.cninnovation.com • csharp.christiannagel.com
  5. 5. Expressions Everywhere (C# 7.0) • Property Accessors • Constructors • Destructors • Event Accessors • Local Functions private string _firstName; public string FirstName { get => _firstName; set => Set(ref _firstName, value); }
  6. 6. Out Vars (C# 7.0) • Declare variable on use string n = "42"; if (string.TryParse(n, out var result) { Console.WriteLine( $"Converting to a number was successful: {result}"); }
  7. 7. Non-Trailing Named Arguments (C# 7.2) • Assign the name to any argument if (Enum.TryParse(weekdayRecommendation.Entity, ignoreCase: true, out DayOfWeek weekday)) { reservation.Weekday = weekday; }
  8. 8. Local Functions (C# 7.0) • Declare functions within methods • Scope within the method • Simplifies Lambda syntax • Helps with parameter checking (yield) public void SomeFunStuff() { int add(int x, int y) => x + y; int result = add(38, 4); Console.WriteLine(result); } public void SomeFunStuff() { Func<int, int, int> add = (x, y) => x + y; int result = add(38, 4); Console.WriteLine(result); }
  9. 9. Throw Expressions (C# 7.0) • Throw in expressions instead of statements private readonly IBooksService _booksService; public BookController(BooksService booksService) { if (booksService == null) { throw new ArgumentNullException(nameof(b)); } _booksService = booksService; } private readonly IBooksService _booksService; public BookController(BooksService booksService) { _booksService = booksService ?? throw new ArgumentNullException(nameof(b)); }
  10. 10. Tuples (C# 7.0) • Combine multiple values • Strong names • Value types var t1 = (n: 42, s: "magic"); int i1 = t1.n; string s1 = t1.s;
  11. 11. Infer Tuple Names (C# 7.1) • Infer tuple names from parameters • Similar to anonymous types var t1 = (racer.FirstName, racer.Wins); int wins = t1.Wins;
  12. 12. Deconstruction (C# 7.0) • Create parts from objects or tuples • Implement Deconstruct Method var p1 = new Person("Tom", "Turbo"); (string firstName, string lastName) = p1;
  13. 13. Pattern Matching (C# 7.0) • is Operator and switch statement extended • Const Pattern • Type Pattern • Var Pattern public void IsSample(object o) { if (o is 42) { } if (o is Person p) { } if (o is var v1) { } } public void PatternMatchingWithSwitchStatement(object o) { switch (o) { case 42: break; case Person p when p.FirstName == "Katharina": break; case Person p: break; case var v: break; } }
  14. 14. Records (C# 8.0 - Planned) • Immutable Type • class or struct public struct Pair(object First, object Second); var p1 = new Pair("one", "two"); var p2 = p1.With(First: "alpha");
  15. 15. Record Implementation (C# 8.0 - Planned) • Implements • IEquatable • GetHashCode • Deconstruct • With public struct Pair(object First, object Second); public struct Pair : IEquatable<Pair> { public object First { get; } public object Second { get; } public Pair(object First, object Second) { this.First = First; this.Second = Second; } public bool Equals(Pair other) => Equals(First, other.First) && Equals(Second, other.Second); public override bool Equals(object other) => (other as Pair)?.Equals(this) == true; public override int GetHashCode() => (First?.GetHashCode()*17 + Second?.GetHashCode()).GetValueOrDefault(); public Pair With(object First = this.First, object Second = this.Second) => new Pair(First, Second); public void Deconstruct(out object First, out object Second) { First = this.First; Second = this.Second; } } var p1 = new Pair("one", "two"); var p2 = p1.With(First: "alpha");
  16. 16. Productivity - Summary • Expressions everywhere • Out vars • Non-trailing named arguments • Local functions • Throw expressions • Tuples • Pattern matching • Records
  17. 17. Performance
  18. 18. Async Streams (C# 8 - Planned) • Exchange stream of data across asynchronous boundary
  19. 19. Async Streams (C# 8 - Planned) • IAsyncDisposable • IAsyncEnumerable • IAsyncEnumerator public interface IAsyncDisposable { Task DisposeAsync(); } public interface IAsyncEnumerable<out T> { IAsyncEnumerator<T> GetAsyncEnumerator(); } public interface IAsyncEnumerator<out T> : IAsyncDiposable { Task<bool> MoveNextAsync(); T Current { get; } }
  20. 20. Using Async Streams (C# 8 Planned) • foreach await IAsyncEnumerator<T> enumerator = enumerable.GetAsyncEnumerator(); try { while (await enumerator.MoveNextAsync()) { Use(enumerator.Current); } } finally { await enumerator.DisposeAsync(); } foreach await (var i in enumerable) { Use(i); }
  21. 21. Async with yield (C# 8 - Planned) • return IAsyncEnumerable static async IAsyncEnumerable<int> MyIterator() { try { for (int i = 0; i < 100; i++) { await Task.Delay(1000); yield return i; } } finally { await Task.Delay(200); Console.WriteLine("finally"); } }
  22. 22. ArrayPool • Reduce work needed by Garbage Collector • Pool of arrays managed by ArrayPool int[] arr = ArrayPool<int>.Shared.Rent(length); //... use array ArrayPool<int>.Shared.Return(arr, clearArray: true);
  23. 23. Reference Semantics • Reference Types • class • Copy By Reference • Use Managed Heap • Value Types • struct • Copy By Value • Use Stack or Managed Heap (Boxing) • GC doesn't need to cleanup the stack
  24. 24. Pass Value Type by… • by value • by reference • by readonly reference • C# 7.2 public void SetItem(int makeACopy) { } public void SetItem(ref int byReference) { byReference = 42; } public void SetItem(in int byReference) { // guarantee no change }
  25. 25. Returning Value Types By Reference • ref return (7.0) • ref local (7.0) • ref readonly return (7.2) • ref readonly local (7.2) public ref int GetItem(int index) { return ref _items[index]; } public ref readonly int GetReadonlyItem(int index) { return ref _items[index]; } ref int x = ref GetItem(4);
  26. 26. .NET Type Categories • Managed Heap • class • Stack or Managed Heap • struct • Stack Only (C# 7.2) • ref struct
  27. 27. Span • Accesses range of native memory, managed heap, stack • Continous range of memory • Reference semantics • Specialized for arrays, strings string text = GetString(); ReadOnlySpan<char> span1 = text.AsSpan(); ReadOnlySpan<char> slice1 = span1.Slice(5, 20); string newString = new string(slice1.ToArray());
  28. 28. Overloads Span and Memory • Parse • System.Random • System.Text.StringBuilder • System.Net.Sockets • Specialized extension methods for arrays and strings public virtual ValueTask<int> ReadAsync( Memory<byte> destination, CancellationToken cancellationToken = default);
  29. 29. Slicing (ranges) – C# 7.3 Planned • Operator .. var slice = source[5..10]; switch (codePoint) { case 1536..1541: break; foreach (var item in 3..5) {
  30. 30. Performance - Summary • Reduce copy needs with value types • Reduce need for unsafe code • Span • ArrayPool
  31. 31. Avoiding Errors
  32. 32. What is the most common exception? • NullReferenceException
  33. 33. Null Conditional Operator (C# 6) • Reduce Null-Checks int? length = customers?.Length; Customer first = customers?[0]; int? count = customers?[0]?.Orders?.Count(); public void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  34. 34. Coalescing Operator (C# 5) • Default values for null int length = customers?.Length ?? 0; public bool CanExecute(object parameter) => _canExecute?.Invoke() ?? true;
  35. 35. Nullable Reference Types (C# 8 Planned) • Helps finding bugs, no guarantee • Flow analysis tracks nullable reference variables • Breaks existing code (opt-in) • Nullability implemented with metadata (ignored by downlevel compilers) • string, T non-nullable • string?, T? nullable
  36. 36. Declare Nullable Reference Types • Default to non-nullable • ? to make reference type nullable class Book { public Book(string title, string? isbn = null) { Title = title; Isbn = isbn; } public string Title; // not null public string? Isbn; // may be null }
  37. 37. Using nullable reference type void M1(string? ns) { Console.WriteLine(ns.Length); // compiler warning void M2(string? ns) { if (ns == null) throw new ArgumentNullException(nameof(ns)); Console.WriteLine(ns.Length); // ok, not null ns = null; Console.WriteLine(ns.Length); // compiler warning void M3(string? ns) { if (ns != null) { Console.WriteLine(ns.Length); // ok
  38. 38. Using non-nullable reference type void M1(string ns) { Console.WriteLine(ns.Length); // ok void M2(Book b) { b.Title = null; // warning string isbn = b.Isbn; // warning – may be null string title = b.Title; // ok
  39. 39. Avoiding Errors - Summary • Get rid of the most common exception
  40. 40. Summary Productivity Performance Avoiding Errors
  41. 41. Questions?
  42. 42. More Information (1) • https://dot.net • https://github.com/dotnet/csharplang • Sample Code: • https://github.com/CNinnovation
  43. 43. More Information (2) • https://github.com/ProfessionalCSharp • https://csharp.christiannagel.com • https://www.cninnovation.com • Training & Coaching
  44. 44. Thank you!
  45. 45. C# 7.0 • Binary literals • Digit separators • Local functions • Out var • Throw expressions
  46. 46. C# 7.1 • Async Main • Generics Pattern Match • Infer Tuple Names • Target-typed default
  47. 47. C# 7.2 • Conditional Ref • Leading Separator • Non-trailing named arguments • Private Protected • Readonly Ref • Span Safety
  48. 48. C# 7.3 Candidates • Generic constraints • delegate, enum • == != on tuples • Blittable • Slicing • Extension methods with parameter modifiers ref and in
  49. 49. 8.0 Candidates • Pattern Matching • Recursive • Subpattern, property… • Range pattern • Deconstruct pattern • Tuple pattern • Default Interface Methods • Records • Nullable Reference Types • Async Streams • UTF8 String Literals

×