Updated Chapter 7 with null reference considerations.
MODIFY
src/Chapter07/Listing07.07.ProtectedMembersAreAccessibleOnlyFromDerivedClasses.cs
src/Chapter07/Listing07.07.ProtectedMembersAreAccessibleOnlyFromDerivedClasses.cs
@@ -9,7 +9,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_07
public class PdaItem
{
public PdaItem(Guid objectKey) => ObjectKey = objectKey;
- protected Guid ObjectKey { get; set; }
+ protected Guid ObjectKey { get; }
}
public class Contact : PdaItem

MODIFY
src/Chapter07/Listing07.19.DefiningAbstractMembers.cs
src/Chapter07/Listing07.19.DefiningAbstractMembers.cs
}
}
- [NotNull][DisallowNull]
- public string? FirstName { get; set; }
- [NotNull][DisallowNull]
- public string? LastName { get; set; }
+ public string FirstName
+ {
+ get
+ {
+ return _FirstName!;
+ }
+ set
+ {
+ _FirstName = value ?? throw new ArgumentNullException(nameof(value)); ;
+ }
+ }
+ private string? _FirstName;
+
+ public string LastName
+ {
+ get
+ {
+ return _LastName!;
+ }
+ set
+ {
+ _LastName = value ?? throw new ArgumentNullException(nameof(value));
+ }
+ }
+ private string? _LastName;
+
public string? Address { get; set; }
public override string GetSummary()

MODIFY
src/Chapter07/Listing07.27.SwitchWithPatternMatching.cs
src/Chapter07/Listing07.27.SwitchWithPatternMatching.cs
@@ -17,7 +17,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_27
string dateText => DateTime.TryParse(
dateText, out DateTime dateTime) ?
(dateTime.Year, dateTime.Month, dateTime.Day) :
- default((int Year, int Month, int Day)?),
+ // default ((int Year, int Month, int Day)?) preferable
+ // not covered until Chapter 12.
+ ((int Year, int Month, int Day)?)null,
_ => null
} is { } date? string.Format(
compositFormatString, date.Year, date.Month, date.Day) : null;

MODIFY
src/Chapter07/Listing07.Later.DataConversionUsingTheAsOperator.cs
src/Chapter07/Listing07.Later.DataConversionUsingTheAsOperator.cs
public class PdaItem
{
- protected Guid ObjectKey { get; set; }
+ protected Guid ObjectKey { get; }
// ...
}

MODIFY
src/Chapter07/Storage.cs
src/Chapter07/Storage.cs
* source code is not available and so polymorphism is
* not possible.
* *****************************************************/
+ public abstract class Storage
+ {
+ protected Storage(long capacity)
+ {
+ Capacity = capacity;
+ }
+ public long Capacity { get; }
+ }
+ public class Dvd : Storage
+ {
+ public Dvd(DvdCapacity capacity) : base((long)capacity)
+ {
+ switch (capacity)
+ {
+ case DvdCapacity.SingleLayeredCapacity:
+ case DvdCapacity.DualLayeredCapacity:
+ break;
+ default:
+ throw new ArgumentException("Only values of 4.7 GB and 8.5 GB are supported.");
+ };
+ }
+ public bool IsInserted { get; set; }
+
+ internal void Eject()
+ {
+ Console.WriteLine("Ejecting the DVD...");
+ }
+ public enum DvdCapacity : long
+ {
+ SingleLayeredCapacity = 47000000000,
+ DualLayeredCapacity = 85000000000
+ }
+ }
+ public class HardDrive : Storage
+ {
+ public HardDrive(long capacity) : base(capacity) { }
+ }
+ public class FloppyDrive : Storage
+ {
+ public FloppyDrive(): base(1440000){}
+ }
+ public class UsbKey : Storage
+ {
+ public UsbKey(long capacity) : base(capacity) { }
+ public bool IsPluggedIn { get; set; }
+ internal void Unload()
+ {
+ Console.WriteLine("Unloading the UsbKey...");
+ }
+ }
}
\ No newline at end of file

Updated Chapter 7 with null reference considerations.