Skip to content

Commit fd2466f

Browse files
authored
Session11 (#52)
* Demos for session 11
1 parent eb3f626 commit fd2466f

File tree

8 files changed

+427
-0
lines changed

8 files changed

+427
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26124.0
5+
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleConsole", "SampleConsole\SampleConsole.csproj", "{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Debug|x64.ActiveCfg = Debug|Any CPU
24+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Debug|x64.Build.0 = Debug|Any CPU
25+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Debug|x86.ActiveCfg = Debug|Any CPU
26+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Debug|x86.Build.0 = Debug|Any CPU
27+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Release|x64.ActiveCfg = Release|Any CPU
30+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Release|x64.Build.0 = Release|Any CPU
31+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Release|x86.ActiveCfg = Release|Any CPU
32+
{DDA80D01-59DC-47F5-9C27-0D93D1DF8AC5}.Release|x86.Build.0 = Release|Any CPU
33+
EndGlobalSection
34+
EndGlobal

sessions/0111-CSharpNine/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Session 11 - New C# v9 Features
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:
4+
5+
1. Record data types
6+
1. Init Only Setters
7+
1. Top Level Programs
8+
1. Pattern Matching Enhancements
9+
1. Target-Typed New Expressions
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using static System.Console;
3+
4+
namespace SampleConsole
5+
{
6+
7+
public class Records
8+
{
9+
10+
public static void Demo()
11+
{
12+
13+
// Create a record object and inspect its string and equals
14+
Demo1();
15+
16+
// Inheritance with records
17+
// Demo2();
18+
19+
// Deconstruction and Cloning
20+
//Demo3();
21+
22+
}
23+
24+
#region Demo 1
25+
26+
public static void Demo1()
27+
{
28+
29+
// Create a new record object and inspect its string
30+
var p = new Person("jeff", "fritz");
31+
WriteLine(p);
32+
33+
// Change the record object
34+
//p.LastName = "smith";
35+
36+
// second record equals first
37+
// var s = new Person("jeff", "fritz");
38+
// WriteLine( p == s);
39+
40+
}
41+
42+
public record Person
43+
{
44+
45+
public string FirstName { get; }
46+
public string LastName { get; }
47+
48+
public Person(string first, string last) =>
49+
(FirstName, LastName) = (first, last);
50+
51+
}
52+
53+
#endregion
54+
55+
#region Demo 2
56+
57+
public static void Demo2()
58+
{
59+
60+
// Records support inheritance
61+
62+
var p = new CoolPerson("jeff", "fritz");
63+
WriteLine(p);
64+
65+
var r = new Rockstar("Freddie", "Mercury", "Queen");
66+
WriteLine(r);
67+
68+
WriteLine(r as CoolPerson);
69+
70+
}
71+
72+
public record CoolPerson(string FirstName, string LastName);
73+
74+
public record Rockstar(string FirstName, string LastName, string BandName) : CoolPerson(FirstName, LastName);
75+
76+
#endregion
77+
78+
#region Demo 3
79+
80+
public static void Demo3() {
81+
82+
var p = new CoolPerson("jeff", "fritz");
83+
var (first, last) = p;
84+
85+
WriteLine($"First: {first} Last: {last}");
86+
87+
// Clone using the WITH expression
88+
// var s = p with { FirstName = "scott"};
89+
// WriteLine(s);
90+
91+
}
92+
93+
#endregion
94+
95+
}
96+
97+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using static System.Console;
3+
4+
namespace SampleConsole {
5+
6+
public class Init {
7+
8+
public static void Demo() {
9+
10+
Demo1();
11+
12+
}
13+
14+
#region Demo1
15+
16+
public static void Demo1() {
17+
18+
/*
19+
var l = new LogEntry {
20+
Severity = LogSeverity.Error,
21+
LogTimestampUtc = DateTime.UtcNow,
22+
Description = "Bad dates..."
23+
};
24+
25+
WriteLine(l);
26+
*/
27+
28+
}
29+
30+
public struct LogEntry {
31+
32+
public readonly LogSeverity Severity;
33+
34+
public readonly DateTime LogTimestampUtc;
35+
36+
public readonly string Description;
37+
38+
public override string ToString()
39+
{
40+
return $"{LogTimestampUtc} - {Severity} - {Description}";
41+
}
42+
43+
}
44+
45+
public enum LogSeverity {
46+
Error,
47+
Warning,
48+
Debug,
49+
Info
50+
}
51+
52+
#endregion
53+
54+
}
55+
56+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
using static System.Console;
2+
3+
namespace SampleConsole
4+
{
5+
6+
public class PatternMatching
7+
{
8+
9+
public static void Demo()
10+
{
11+
12+
var pm = new PatternMatching();
13+
//pm.Basics();
14+
//pm.BasicSwitch();
15+
//pm.PropertyPattern();
16+
//pm.RockPaperScissors("rock", "scissors");
17+
pm.NineSimplePattern();
18+
pm.NineSimplePattern();
19+
20+
}
21+
22+
public void Basics()
23+
{
24+
25+
// Match on the SHAPE of an object
26+
object data = 1;
27+
28+
if (data is int i) WriteLine($"That was an integer: {i}");
29+
else if (data is double d) WriteLine("Ooh, a double!");
30+
else WriteLine("Please give me a number");
31+
32+
}
33+
34+
public void BasicSwitch()
35+
{
36+
37+
object data = 0;
38+
39+
switch (data)
40+
{
41+
42+
case 0:
43+
WriteLine("Zero");
44+
break;
45+
case double d:
46+
WriteLine("Double!");
47+
break;
48+
case int n when n > 0:
49+
WriteLine("A positive integer");
50+
break;
51+
case null:
52+
WriteLine("Give me a value!");
53+
break;
54+
default:
55+
WriteLine("You're boring...");
56+
break;
57+
}
58+
59+
}
60+
61+
public void PropertyPattern()
62+
{
63+
64+
// Calculate toll
65+
Vehicle v = null;
66+
67+
var toll = v switch
68+
{
69+
{ Wheels: 2 } => 0.25,
70+
{ Wheels: 4 } => 0.50,
71+
Truck t when t.TotalWeight < 10000 => 1.00,
72+
_ => 2.00
73+
};
74+
75+
}
76+
77+
public static string RockPaperScissors(string first, string second)
78+
=> (first, second) switch
79+
{
80+
("rock", "paper") => "rock is covered by paper. Paper wins.",
81+
("rock", "scissors") => "rock breaks scissors. Rock wins.",
82+
("paper", "rock") => "paper covers rock. Paper wins.",
83+
("paper", "scissors") => "paper is cut by scissors. Scissors wins.",
84+
("scissors", "rock") => "scissors is broken by rock. Rock wins.",
85+
("scissors", "paper") => "scissors cuts paper. Scissors wins.",
86+
(_, _) => "tie"
87+
};
88+
89+
90+
public void NineSimplePattern() {
91+
92+
// Calculate toll
93+
Vehicle v = null;
94+
95+
var toll = v switch
96+
{
97+
{ Wheels: 2 } => 0.25,
98+
{ Wheels: 4 } => 0.50,
99+
Truck => 1.00,
100+
_ => 2.00
101+
};
102+
103+
104+
}
105+
106+
public void NineRelationalPatterns() {
107+
108+
// Calculate toll
109+
Truck t = new Truck { TotalWeight=9500 };
110+
111+
var toll = t.TotalWeight switch
112+
{
113+
< 5000 => 1.00,
114+
< 10000 => 2.00,
115+
> 50000 => 5.00,
116+
_ => 3.00
117+
};
118+
119+
}
120+
121+
public void NineLogicalPatterns() {
122+
123+
// Calculate toll
124+
Truck t = new Truck { TotalWeight=9500 };
125+
126+
// and or not
127+
128+
var toll = t.TotalWeight switch
129+
{
130+
< 5000 => 1.00,
131+
< 10000 and >= 5000 => 2.00,
132+
> 50000 => 5.00,
133+
_ => 3.00
134+
};
135+
136+
}
137+
138+
#region Objects
139+
140+
public abstract class Vehicle
141+
{
142+
143+
public int Passengers { get; set; }
144+
145+
public int Wheels { get; set; }
146+
147+
}
148+
149+
public class Car : Vehicle
150+
{
151+
public Car()
152+
{
153+
Wheels = 4;
154+
}
155+
}
156+
157+
public class Truck : Vehicle
158+
{
159+
public int TotalWeight { get; set; }
160+
}
161+
162+
#endregion
163+
164+
}
165+
166+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Drawing;
3+
using System.Collections.Generic;
4+
using static System.Console;
5+
6+
namespace SampleConsole {
7+
8+
public class NewTarget {
9+
10+
public static void Demo() {
11+
12+
var t = new NewTarget();
13+
t.Demo1();
14+
15+
}
16+
17+
public void Demo1() {
18+
19+
List<int> myList = new();
20+
21+
// Repeated object initializers
22+
Point[] numbers = {new (1,1), new(1,2), new(1,3)};
23+
WriteLine(numbers.Length);
24+
25+
}
26+
27+
}
28+
29+
}

0 commit comments

Comments
 (0)