Skip to content

Commit 6366be7

Browse files
Merge pull request #125 from ProfessionalCSharp/124-target-typed-new
124 target typed new
2 parents eda3e92 + bd01501 commit 6366be7

File tree

7 files changed

+35
-11
lines changed

7 files changed

+35
-11
lines changed

1_CS/Collections/DictionarySample/Employee.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
public record Employee
1+
namespace DictionarySample;
2+
3+
public record Employee
24
{
35
private readonly string _name;
46
private readonly decimal _salary;

1_CS/Collections/DictionarySample/EmployeeId.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
public class EmployeeIdException : Exception
1+
namespace DictionarySample;
2+
3+
public class EmployeeIdException : Exception
24
{
35
public EmployeeIdException(string message) : base(message) { }
46
}

1_CS/Collections/DictionarySample/Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
EmployeeId idKyle = new("J18");
2-
Employee kyle = new Employee(idKyle, "Kyle Bush", 138_000.00m );
1+
using DictionarySample;
2+
3+
EmployeeId idKyle = new("J18");
4+
Employee kyle = new(idKyle, "Kyle Bush", 138_000.00m );
35

46
EmployeeId idMartin = new("J19");
57
Employee martin = new(idMartin, "Martin Truex Jr", 73_000.00m);
68

79
EmployeeId idKevin = new("S4");
810
Employee kevin = new(idKevin, "Kevin Harvick", 116_000.00m);
911

10-
EmployeeId idDenny = new EmployeeId("J11");
11-
Employee denny = new Employee(idDenny, "Denny Hamlin", 127_000.00m);
12+
EmployeeId idDenny = new("J11");
13+
Employee denny = new(idDenny, "Denny Hamlin", 127_000.00m);
1214

1315
EmployeeId idJoey = new("T22");
1416
Employee joey = new(idJoey, "Joey Logano", 96_000.00m);

1_CS/Collections/ImmutableCollectionSample/GlobalUsings.cs

Lines changed: 0 additions & 1 deletion
This file was deleted.

1_CS/Collections/ImmutableCollectionSample/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
SimpleArrayDemo();
1+
using System.Collections.Immutable;
2+
3+
SimpleArrayDemo();
24
ImmutableList<Account> accounts = CreateImmutableList();
35
UsingABuilder(accounts);
46
LinqDemo();

1_CS/Collections/ListSamples/Program.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
Console.WriteLine("object not found in collection");
4747
}
4848

49-
List<Racer> racers2 = new(new Racer[] {
50-
new(12, "Jochen", "Rindt", "Austria", 6),
51-
new(22, "Ayrton", "Senna", "Brazil", 41) });
49+
List<Racer> racers2 = new(
50+
new Racer[]
51+
{
52+
new(12, "Jochen", "Rindt", "Austria", 6),
53+
new(22, "Ayrton", "Senna", "Brazil", 41)
54+
});

Updates.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ Page 190, the event name should be `NewCarCreated` instead of `NewCarInfo`:
4343

4444
Thanks to [@DanielNikoofar](https://github.com/DanielNikoofar) for reporting this issue!
4545

46+
## Chapter 11, Tasks and Asynchrnous Programming
47+
48+
Page 296 shows this source code:
49+
50+
```csharp
51+
private readonly static Dictionary<string, string> names = new Dictionary<string, string>();
52+
```
53+
54+
With C# 9 and **target-typed new expressions**, the code can be written as shown:
55+
56+
```csharp
57+
private readonly static Dictionary<string, string> names = new();
58+
```
59+
4660
## Chapter 20, Security
4761

4862
Page 560, the command

0 commit comments

Comments
 (0)