Added item for page 244

This commit is contained in:
Mark J Price 2022-11-10 22:01:07 +00:00
parent 4d97515083
commit 74baaf5669
5 changed files with 52 additions and 15 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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);

View file

@ -1,7 +0,0 @@
namespace Packt.Shared;
public class ImmutablePerson
{
//public string? FirstName { get; init; }
//public string? LastName { get; init; }
}

View file

@ -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}.");