From 74baaf5669bde29c201a0cea4dbc91bbe5a4c611 Mon Sep 17 00:00:00 2001 From: Mark J Price Date: Thu, 10 Nov 2022 22:01:07 +0000 Subject: [PATCH] Added item for page 244 --- docs/errata/README.md | 2 +- docs/errata/errata.md | 9 +++++- .../Chapter05/PacktLibraryModern/Records.cs | 18 +++++++++++ .../PacktLibraryNetStandard2/Records.cs | 7 ----- vs4win/Chapter05/PeopleApp/Program.cs | 31 +++++++++++++++---- 5 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 vs4win/Chapter05/PacktLibraryModern/Records.cs delete mode 100644 vs4win/Chapter05/PacktLibraryNetStandard2/Records.cs diff --git a/docs/errata/README.md b/docs/errata/README.md index e9b32a8..83884ba 100644 --- a/docs/errata/README.md +++ b/docs/errata/README.md @@ -2,7 +2,7 @@ If you find any mistakes in the seventh edition, *C# 11 and .NET 7 - Modern Cross-Platform Development Fundamentals*, or if you have suggestions for improvements, then please [raise an issue in this repository](https://github.com/markjprice/cs11dotnet7/issues) or email me at markjprice (at) gmail.com. -[**Errata** (6 items)](errata.md): Typos, tool user interface changes, or mistakes in code that would cause a compilation error that prevents a successful build. +[**Errata** (7 items)](errata.md): Typos, tool user interface changes, or mistakes in code that would cause a compilation error that prevents a successful build. [**Improvements** (1 item)](improvements.md): Changes to text or code that would improve the content. These are optional. diff --git a/docs/errata/errata.md b/docs/errata/errata.md index dd0a6e0..56a68d9 100644 --- a/docs/errata/errata.md +++ b/docs/errata/errata.md @@ -1,4 +1,4 @@ -**Errata (6 items)** +**Errata (7 items)** If you find any mistakes, then please [raise an issue in this repository](https://github.com/markjprice/cs11dotnet7/issues) or email me at markjprice (at) gmail.com. @@ -10,6 +10,7 @@ If you find any mistakes, then please [raise an issue in this repository](https: - [Page 83 - Formatting using numbered positional arguments](#page-83---formatting-using-numbered-positional-arguments) - [Page 86 - Getting text input from the user](#page-86---getting-text-input-from-the-user) - [Page 188 - Running unit tests using Visual Studio Code](#page-188---running-unit-tests-using-visual-studio-code) +- [Page 244 - Init-only properties](#page-244---init-only-properties) # Page 4, 8 - Pros and cons of the .NET Interactive Notebooks extension, Downloading and installing Visual Studio Code @@ -81,3 +82,9 @@ In Step 3, I wrote, "For the `firstName` variable" when I should have written, " > Thanks to [kwatsonkairosmgt](https://github.com/kwatsonkairosmgt) for raising this [issue on 27 October 2022](https://github.com/markjprice/cs10dotnet6/issues/106). In Step 1, the project name `CalculatorLibUnitTest` should be `CalculatorLibUnitTests`. + +# Page 244 - Init-only properties + +> Thanks to Bob Molloy for raising this issue via email. + +In Step 1, I say to add a new file named `Records.cs` to the `PacktLibraryNetStandard2` project/folder. I should have said to the `PacktLibraryModern` project/folder. diff --git a/vs4win/Chapter05/PacktLibraryModern/Records.cs b/vs4win/Chapter05/PacktLibraryModern/Records.cs new file mode 100644 index 0000000..096d29d --- /dev/null +++ b/vs4win/Chapter05/PacktLibraryModern/Records.cs @@ -0,0 +1,18 @@ +namespace Packt.Shared; + +public class ImmutablePerson +{ + public string? FirstName { get; init; } + public string? LastName { get; init; } +} + +public record ImmutableVehicle +{ + public int Wheels { get; init; } + public string? Color { get; init; } + public string? Brand { get; init; } +} + +// simpler way to define a record +// auto-generates the properties, constructor, and deconstructor +public record ImmutableAnimal(string Name, string Species); \ No newline at end of file diff --git a/vs4win/Chapter05/PacktLibraryNetStandard2/Records.cs b/vs4win/Chapter05/PacktLibraryNetStandard2/Records.cs deleted file mode 100644 index bfb2a46..0000000 --- a/vs4win/Chapter05/PacktLibraryNetStandard2/Records.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Packt.Shared; - -public class ImmutablePerson -{ - //public string? FirstName { get; init; } - //public string? LastName { get; init; } -} diff --git a/vs4win/Chapter05/PeopleApp/Program.cs b/vs4win/Chapter05/PeopleApp/Program.cs index f80431d..f57c114 100644 --- a/vs4win/Chapter05/PeopleApp/Program.cs +++ b/vs4win/Chapter05/PeopleApp/Program.cs @@ -293,9 +293,28 @@ foreach (Passenger passenger in passengers) WriteLine($"Flight costs {flightCost:C} for {passenger}"); } -//ImmutablePerson jeff = new() -//{ -// FirstName = "Jeff", -// LastName = "Winger" -//}; -//jeff.FirstName = "Geoff"; +ImmutablePerson jeff = new() +{ + FirstName = "Jeff", + LastName = "Winger" +}; +// We cannot set the properties after initialization because they +// are init-only. +// jeff.FirstName = "Geoff"; + +ImmutableVehicle car = new() +{ + Brand = "Mazda MX-5 RF", + Color = "Soul Red Crystal Metallic", + Wheels = 4 +}; + +ImmutableVehicle repaintedCar = car + with { Color = "Polymetal Grey Metallic" }; + +WriteLine($"Original car color was {car.Color}."); +WriteLine($"New car color is {repaintedCar.Color}."); + +ImmutableAnimal oscar = new("Oscar", "Labrador"); +var (who, what) = oscar; // calls Deconstruct method +WriteLine($"{who} is a {what}.");