|
1 | 1 | # Session 11 - New C# v9 Features
|
2 | 2 |
|
3 |
| -With the new release of .NET 5 in November 2020, a new version of the C# programming language was shipped. Let's take a look at five of the largest updates to the language in this session: |
| 3 | +With the new release of .NET 5 in November 2020, a new version of the C# programming language was shipped labeled C# v9. Let's take a look at five of the largest updates to the language in this session: |
4 | 4 |
|
5 | 5 | 1. Record data types
|
6 | 6 | 1. Init Only Setters
|
7 | 7 | 1. Top Level Programs
|
8 | 8 | 1. Pattern Matching Enhancements
|
9 | 9 | 1. Target-Typed New Expressions
|
| 10 | + |
| 11 | +[Official Documentation](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9?WT.mc_id=visualstudio-twitch-jefritz) of these updates are available online. |
| 12 | + |
| 13 | +## Record Data Types |
| 14 | + |
| 15 | +Records are a new reference type that synthesizes methods to provide semantics for equality. Records are immutable (cannot be modified) by default. |
| 16 | + |
| 17 | +The compiler will generate the following methods for you in a record type: |
| 18 | + |
| 19 | +- Methods for value-based equality comparisons |
| 20 | +- GetHasCode() |
| 21 | +- Copy and Clone members |
| 22 | +- `PrintMembers` and `ToString()` |
| 23 | + |
| 24 | +Take a look at the samples in `1-Records.cs` to interact with the `record` keyword and types. |
| 25 | + |
| 26 | +## Init Only Setters |
| 27 | + |
| 28 | +Classes can contain fields marked `readonly` that allow their values to only be set through a constructor. This can be a bit limiting if you'd like to be able to set values through an intializer in the format: |
| 29 | + |
| 30 | +```csharp |
| 31 | +public class Person { |
| 32 | + public readonly string FirstName; |
| 33 | +} |
| 34 | + |
| 35 | +var p = new Person { |
| 36 | + FirstName = "Jeff" |
| 37 | +}; |
| 38 | +``` |
| 39 | + |
| 40 | +That doesn't work... neither does this: |
| 41 | + |
| 42 | +```csharp |
| 43 | +public class Person { |
| 44 | + public string FirstName { get; private set; } |
| 45 | +} |
| 46 | + |
| 47 | +var p = new Person { |
| 48 | + FirstName = "Jeff" |
| 49 | +}; |
| 50 | +``` |
| 51 | + |
| 52 | +We can now introduce the `init` scope modifier for a property that allows is to only be set in a constructor OR an initializer. |
| 53 | + |
| 54 | +```csharp |
| 55 | +public class Person { |
| 56 | + public string FirstName { get; init set; } |
| 57 | +} |
| 58 | + |
| 59 | +var p = new Person { |
| 60 | + FirstName = "Jeff" |
| 61 | +}; |
| 62 | +``` |
| 63 | + |
| 64 | +Try out the initializers in `2-Init.cs` |
| 65 | + |
| 66 | +## Top Level Programs |
| 67 | + |
| 68 | +You can now place a file in your project with statements not enclosed in a namespace, a class, or a method. This can help with not just learning, but running methods quickly in a rapid-development scenario. |
| 69 | + |
| 70 | +```csharp |
| 71 | +System.Console.WriteLine("Hello World"); |
| 72 | +``` |
| 73 | + |
| 74 | +```dotnetcli |
| 75 | +dotnet watch run |
| 76 | +``` |
| 77 | + |
| 78 | +You can reach directly into your application and execute methods on classes. When you're done testing and tinkering, you can remove the top-level file and everything will continue running with a normal `public static void Main` signature. |
| 79 | + |
| 80 | +## Pattern Matching Enhancements |
| 81 | + |
| 82 | +Pattern matching was a comparison technique introduced in C# 7 that allows you to test the _SHAPE_ of an object in an `if` or `switch` statement. |
| 83 | + |
| 84 | +MORE DOCS ABOUT PATTERN-MATCHING FEATURES |
| 85 | +https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7?WT.mc_id=visualstudio-twitch-jefritz#pattern-matching |
| 86 | + |
| 87 | + |
| 88 | +https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8?WT.mc_id=visualstudio-twitch-jefritz#more-patterns-in-more-places |
| 89 | + |
| 90 | + |
| 91 | +## Target-Typed New Expressions |
| 92 | + |
| 93 | +Constructors can now be simplified when the type is known to just a `new()` statement. |
| 94 | + |
| 95 | +Check the samples in `5-NewTarget.cs` |
| 96 | + |
0 commit comments