Skip to content

Commit ad66015

Browse files
authored
Session 11 content (#53)
* Added Records * Init samples ready * Completed demos for session 11 * Added content to README * Updated with final values from session 11
1 parent fd2466f commit ad66015

File tree

7 files changed

+300
-168
lines changed

7 files changed

+300
-168
lines changed

sessions/0111-CSharpNine/README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,96 @@
11
# Session 11 - New C# v9 Features
22

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:
44

55
1. Record data types
66
1. Init Only Setters
77
1. Top Level Programs
88
1. Pattern Matching Enhancements
99
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+

sessions/0111-CSharpNine/SampleConsole/1-Records.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static void Demo()
1111
{
1212

1313
// Create a record object and inspect its string and equals
14-
Demo1();
14+
//Demo1();
1515

1616
// Inheritance with records
1717
// Demo2();
@@ -28,14 +28,30 @@ public static void Demo1()
2828

2929
// Create a new record object and inspect its string
3030
var p = new Person("jeff", "fritz");
31-
WriteLine(p);
31+
// WriteLine(p.FirstName);
32+
33+
//WriteLine(new PersonClass("jeff","fritz"));
3234

3335
// Change the record object
3436
//p.LastName = "smith";
3537

3638
// second record equals first
37-
// var s = new Person("jeff", "fritz");
38-
// WriteLine( p == s);
39+
var s = new Person("jeff", "fritz");
40+
WriteLine( p == s);
41+
42+
}
43+
44+
public class PersonClass {
45+
public string FirstName { get; set; }
46+
public string LastName { get; set; }
47+
48+
public override string ToString()
49+
{
50+
return $"{FirstName} {LastName}";
51+
}
52+
53+
public PersonClass(string first, string last) =>
54+
(FirstName, LastName) = (first, last);
3955

4056
}
4157

@@ -59,11 +75,11 @@ public static void Demo2()
5975

6076
// Records support inheritance
6177

62-
var p = new CoolPerson("jeff", "fritz");
63-
WriteLine(p);
78+
// var p = new CoolPerson("jeff", "fritz");
79+
// WriteLine(p);
6480

6581
var r = new Rockstar("Freddie", "Mercury", "Queen");
66-
WriteLine(r);
82+
// WriteLine(r);
6783

6884
WriteLine(r as CoolPerson);
6985

@@ -85,8 +101,9 @@ public static void Demo3() {
85101
WriteLine($"First: {first} Last: {last}");
86102

87103
// Clone using the WITH expression
88-
// var s = p with { FirstName = "scott"};
89-
// WriteLine(s);
104+
var s = p with { FirstName="scott" };
105+
WriteLine(s);
106+
WriteLine(p);
90107

91108
}
92109

sessions/0111-CSharpNine/SampleConsole/2-Init.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,23 @@ public static void Demo() {
1515

1616
public static void Demo1() {
1717

18-
/*
1918
var l = new LogEntry {
2019
Severity = LogSeverity.Error,
2120
LogTimestampUtc = DateTime.UtcNow,
2221
Description = "Bad dates..."
2322
};
2423

2524
WriteLine(l);
26-
*/
2725

2826
}
2927

3028
public struct LogEntry {
3129

32-
public readonly LogSeverity Severity;
30+
public LogSeverity Severity {get; init;}
3331

34-
public readonly DateTime LogTimestampUtc;
32+
public DateTime LogTimestampUtc {get; init;}
3533

36-
public readonly string Description;
34+
public string Description {get; init;}
3735

3836
public override string ToString()
3937
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
using System;
3+
using SampleConsole;
4+
5+
Console.WriteLine("This is a top-level program");
6+
Console.WriteLine($"These are the arguments passed in: {string.Join(',',args)}");
7+
Records.Demo3();
8+
9+
*/

0 commit comments

Comments
 (0)