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 sharp 8.0 new features

132 views

Published on

C# 8.0 new features

Published in: Software
  • Be the first to comment

  • Be the first to like this

C sharp 8.0 new features

  1. 1. C# 8.0 new features
  2. 2. Who Am I? Miguel has over 10 years of experience with Microsoft Technologies. Specialized with many things in the Microsoft ecosystem, including C#, F#, and Azure, he likes to blog about his engineering notes and technology in general. He is also very involved in the Montréal msdevmtl community where he is a co-organizer. NEXUS INNOVATIONS - PRÉSENTATION 2 https://blog.miguelbernard.com https://www.linkedin.com/in/miguelbernard/ @MiguelBernard88 https://github.com/mbernard
  3. 3. New features • Readonly members • Default interface methods • Pattern matching enhancements • Using declarations • Static local functions • Disposable ref structs • Nullable reference types • Asynchronous streams • Indices and ranges • Null-coalescing assignment • Unmanaged constructed types • Stackalloc in nested expressions • Enhancement of interpolated verbatim strings NEXUS INNOVATIONS - PRÉSENTATION 3
  4. 4. Nullable reference types To enable it • In .csproj • <LangVersion>8.0</LangVersion> • <Nullable>Enable</Nullable> • In code • #nullable enable • #nullable restore NEXUS INNOVATIONS - PRÉSENTATION 4
  5. 5. Nullable reference types ? nullable operator ! Null forgiving operator i.e. NEXUS INNOVATIONS - PRÉSENTATION 5 string? a = null; a!.Length;
  6. 6. Nullable reference types DEMO NEXUS INNOVATIONS - PRÉSENTATION 6
  7. 7. Pattern matching enhancements Use case of a Toll service V1 -Price per vehicle type • 🚗Car -> 2$ • 🚕Taxi -> 3.50$ • 🚌Bus -> 5$ • 🚚Truck -> 10$ NEXUS INNOVATIONS - PRÉSENTATION 7
  8. 8. Pattern matching enhancements V2 - Price considering occupancy • Car and taxi • No passengers -> +0.50 $ • 2 passengers -> -0.50 $ • 3 or more passengers -> -1$ NEXUS INNOVATIONS - PRÉSENTATION 8
  9. 9. Pattern matching enhancements V3 - Price considering occupancy • Car and taxi • No passengers -> +0.50 $ • 2 passengers -> -0.50 $ • 3 or more passengers -> -1$ • Bus • Less than 50% full -> +2$ • More than 90% full -> -1$ NEXUS INNOVATIONS - PRÉSENTATION 9
  10. 10. Pattern matching enhancements V4 - Price considering weight • > 5000 lbs -> +5$ • < 3000 lbs -> -2$ NEXUS INNOVATIONS - PRÉSENTATION 10
  11. 11. Pattern matching enhancements V5 – Refactor Car and Taxi! NEXUS INNOVATIONS - PRÉSENTATION 11
  12. 12. Asynchronous streams To have an async stream you need 3 properties • It’s declared with the ‘async’ modifier • It returns an IAsyncEnumerable<T> • The method contains ‘yield return’ statements NEXUS INNOVATIONS - PRÉSENTATION 12
  13. 13. Asynchronous streams NEXUS INNOVATIONS - PRÉSENTATION 13 internal async IAsyncEnumerable<int> GenerateSequence() { for (int i = 0; i < 20; i++) { // every 3 elements, wait 2 seconds: if (i % 3 == 0) await Task.Delay(3000); yield return i; } } internal async Task<int> ConsumeStream() { await foreach (var number in GenerateSequence()) { Console.WriteLine($"The time is {DateTime.Now:hh:mm:ss}. Retrieved {number}"); } return 0; }
  14. 14. Indices and ranges New operators • The index from end operator ^, which specifies that an index is relative to the end of the sequence • The range operator .., which specifies the start and end of a range as its operands NEXUS INNOVATIONS - PRÉSENTATION 14
  15. 15. Indices and ranges Notes • The ^0 index is the same as sequence[sequence.Length]. • Note that sequence[^0] does throw an exception, just as sequence[sequence.Length] does • For any number n, the index ^n is the same as sequence.Length – n • The range [0..^0] represents the entire range, just as [0..sequence.Length] represents the entire range. NEXUS INNOVATIONS - PRÉSENTATION 15
  16. 16. Indices and ranges NEXUS INNOVATIONS - PRÉSENTATION 16 private string[] words = new string[] { // index from start index from end "The", // 0 ^9 "quick", // 1 ^8 "brown", // 2 ^7 "fox", // 3 ^6 "jumped", // 4 ^5 "over", // 5 ^4 "the", // 6 ^3 "lazy", // 7 ^2 "dog" // 8 ^1 };
  17. 17. Indices and ranges NEXUS INNOVATIONS - PRÉSENTATION 17 var allWords = words[..]; // contains "The" through "dog". var firstPhrase = words[..4]; // contains "The" through "fox" var lastPhrase = words[6..]; // contains "the, "lazy" and "dog" var lazyDog = words[^2..^0]; Index the = ^3; words[the]; Range phrase = 1..4; words[phrase];
  18. 18. Default interface methods Demo! NEXUS INNOVATIONS - PRÉSENTATION 18
  19. 19. Static local functions NEXUS INNOVATIONS - PRÉSENTATION 19 int M() { int y = 5; int x = 7; return Add(x, y); static int Add(int left, int right) => left + right; } You can now add the static modifier to local functions to ensure that local function doesn't capture (reference) any variables from the enclosing scope. Doing so generates CS8421, "A static local function can't contain a reference to <variable>."
  20. 20. Null-coalescing assignment C# 8.0 introduces the null-coalescing assignment operator ??=. You can use the ??= operator to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. NEXUS INNOVATIONS - PRÉSENTATION 20 List<int> numbers = null; int? a = null; (numbers ??= new List<int>()).Add(5); Console.WriteLine(string.Join(" ", numbers)); // output: 5 numbers.Add(a ??= 0); Console.WriteLine(string.Join(" ", numbers)); // output: 5 0 Console.WriteLine(a); // output: 0
  21. 21. Using declarations Using variables are automatically disposed at the end of the current scope NEXUS INNOVATIONS - PRÉSENTATION 21 public void Method() { using var file = new StreamWriter(“myFile.txt”); …logic here… } // Disposed here public void Method() { using(var file = new StreamWriter(“myFile.txt”)) { …logic here… } // Disposed here }
  22. 22. Enhancement of interpolated verbatim strings Order of the $ and @ tokens in interpolated verbatim strings can be any: both $@"..." and @$"..." are valid interpolated verbatim strings In earlier C# versions, the $ token must appear before the @ token NEXUS INNOVATIONS - PRÉSENTATION 22
  23. 23. Readonly members • Only applicable for struct • Advantages • The compilier can now enforce your intent • Doing so will enable to compiler to perform performance optimizations on your code NEXUS INNOVATIONS - PRÉSENTATION 23 public struct Point { public double X { get; set; } public double Y { get; set; } public readonly double Distance => Math.Sqrt(X * X + Y * Y); public readonly override string ToString() => $"({X}, {Y}) is {Distance} from the origin"; }
  24. 24. Unmanaged constructed types NEXUS INNOVATIONS - PRÉSENTATION 24 public struct Coords<T> where T : unmanaged { public T X; public T Y; } Span<Coords<int>> coordinates = stackalloc[] { new Coords<int> { X = 0, Y = 0 }, new Coords<int> { X = 0, Y = 3 }, new Coords<int> { X = 4, Y = 0 } };
  25. 25. Stackalloc in nested expressions Starting with C# 8.0, if the result of a stackalloc expression is of the Span<T> or ReadOnlySpan<T> type, you can use the stackalloc expression in other expressions NEXUS INNOVATIONS - PRÉSENTATION 25 Span<int> numbers = stackalloc[] { 1, 2, 3, 4, 5, 6 }; var ind = numbers.IndexOfAny(stackalloc[] { 2, 4, 6 ,8 }); Console.WriteLine(ind); // output: 1
  26. 26. Disposable ref structs A struct declared with the ref modifier may not implement any interfaces and so can't implement IDisposable. Therefore, to enable a ref struct to be disposed, it must have an accessible `void Dispose()` method. This feature also applies to readonly ref struct declarations. NEXUS INNOVATIONS - PRÉSENTATION 26 ref struct Book { public void Dispose() { } }
  27. 27. References • https://github.com/mbernard/csharp8sample • https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8 • https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface- methods-versions NEXUS INNOVATIONS - PRÉSENTATION 27
  28. 28. Questions? Questions? NEXUS INNOVATIONS - PRÉSENTATION 28 https://blog.miguelbernard.com https://www.linkedin.com/in/miguelbernard/ @MiguelBernard88 https://github.com/mbernard
  29. 29. 1751 Richardson, suite, 4.500 H3K 1G6, Montréa, Canada NEXUS INNOVATIONS - PRÉSENTATION 29

×