Updated Chapter 17 for nullability.
MODIFY
src/Chapter17/Listing17.01.UsingList.cs
src/Chapter17/Listing17.01.UsingList.cs
@@ -7,17 +7,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_01
{
public static void Main()
{
- List<string> list = new List<string>();
-
- // Lists automatically expand as elements
- // are added
- list.Add("Sneezy");
- list.Add("Happy");
- list.Add("Dopey");
- list.Add("Doc");
- list.Add("Sleepy");
- list.Add("Bashful");
- list.Add("Grumpy");
+ List<string> list = new List<string>
+ { "Sneezy", "Happy", "Dopey", "Doc", "Sleepy", "Bashful", "Grumpy"};
list.Sort();

MODIFY
src/Chapter17/Listing17.02.ImplementingIComparer.cs
src/Chapter17/Listing17.02.ImplementingIComparer.cs
@@ -7,19 +7,21 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_02
{
public static void Main()
{
- List<Contact> list = new List<Contact>();
+ List<Contact> list = new List<Contact>
+ {
// Lists automatically expand as elements
// are added
- list.Add(new Contact("Sneezy", "Dwarf"));
- list.Add(new Contact("Happy", "Dwarf"));
- list.Add(new Contact("Dopey", "Dwarf"));
- list.Add(new Contact("Doc", "Dwarf"));
- list.Add(new Contact("Sleepy", "Dwarf"));
- list.Add(new Contact("Bashful", "Dwarf"));
- list.Add(new Contact("Grumpy", "Dwarf"));
- list.Add(new Contact("Duplicate", "Dwarf"));
- list.Add(new Contact("Duplicate", "Scarf"));
+ new Contact("Sneezy", "Dwarf"),
+ new Contact("Happy", "Dwarf"),
+ new Contact("Dopey", "Dwarf"),
+ new Contact("Doc", "Dwarf"),
+ new Contact("Sleepy", "Dwarf"),
+ new Contact("Bashful", "Dwarf"),
+ new Contact("Grumpy", "Dwarf"),
+ new Contact("Duplicate", "Dwarf"),
+ new Contact("Duplicate", "Scarf")
+ };
IComparer<Contact> comparer = new NameComparison();
@@ -34,7 +36,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_02
class NameComparison : IComparer<Contact>
{
- public int Compare(Contact x, Contact y)
+ public int Compare(Contact? x, Contact? y)
{
if(Object.ReferenceEquals(x, y))
return 0;
@@ -48,7 +50,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_02
return result;
}
- private static int StringCompare(string x, string y)
+ private static int StringCompare(string? x, string? y)
{
if(Object.ReferenceEquals(x, y))
return 0;

MODIFY
src/Chapter17/Listing17.08.ImplementingIEqualityComparer.cs
src/Chapter17/Listing17.08.ImplementingIEqualityComparer.cs
@@ -5,7 +5,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_08
class ContactEquality : IEqualityComparer<Contact>
{
- public bool Equals(Contact x, Contact y)
+ public bool Equals(Contact? x, Contact? y)
{
if(Object.ReferenceEquals(x, y))
return true;
@@ -17,7 +17,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_08
public int GetHashCode(Contact x)
{
- if(Object.ReferenceEquals(x, null))
+ if(x is null)
return 0;
int h1 = x.FirstName == null ? 0 : x.FirstName.GetHashCode();

MODIFY
src/Chapter17/Listing17.10.ChangingTheIndexersDefaultName.cs
src/Chapter17/Listing17.10.ChangingTheIndexersDefaultName.cs
@@ -40,18 +40,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_10
{
get
{
- switch(index)
- {
- case PairItem.First:
- return First;
- case PairItem.Second:
- return Second;
- default:
- throw new NotImplementedException(
- string.Format(
- "The enum {0} has not been implemented",
- index.ToString()));
- }
+ return index switch
+ {
+ PairItem.First => First,
+ PairItem.Second => Second,
+ _ => throw new NotImplementedException(
+ $"The enum {index} has not been implemented"),
+ };
}
}
}

MODIFY
src/Chapter17/Listing17.11.DefiningAnIndexOperatorWithVariableParameters.cs
src/Chapter17/Listing17.11.DefiningAnIndexOperatorWithVariableParameters.cs
@@ -20,7 +20,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_11
/// <example>
/// familyTree.SubItems.Second.SubItems[PairItem.First].Value
/// </example>
- public BinaryTree<T> this[params PairItem[] branches]
+ public BinaryTree<T> this[params PairItem[]? branches]
{
get
{
@@ -61,23 +61,24 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_11
{
// JFK
var jfkFamilyTree = new BinaryTree<string>(
- "John Fitzgerald Kennedy");
-
- jfkFamilyTree.SubItems = new Pair<BinaryTree<string>>(
- new BinaryTree<string>("Joseph Patrick Kennedy"),
- new BinaryTree<string>("Rose Elizabeth Fitzgerald"));
-
+ "John Fitzgerald Kennedy")
+ {
+ SubItems = new Pair<BinaryTree<string>>(
+ new BinaryTree<string>("Joseph Patrick Kennedy")
+ {
// Grandparents (Father's side)
- jfkFamilyTree.SubItems.First.SubItems =
- new Pair<BinaryTree<string>>(
+ SubItems = new Pair<BinaryTree<string>>(
new BinaryTree<string>("Patrick Joseph Kennedy"),
- new BinaryTree<string>("Mary Augusta Hickey"));
-
+ new BinaryTree<string>("Mary Augusta Hickey"))
+ },
+ new BinaryTree<string>("Rose Elizabeth Fitzgerald")
+ {
// Grandparents (Mother's side)
- jfkFamilyTree.SubItems.Second.SubItems =
- new Pair<BinaryTree<string>>(
+ SubItems = new Pair<BinaryTree<string>>(
new BinaryTree<string>("John Francis Fitzgerald"),
- new BinaryTree<string>("Mary Josephine Hannon"));
+ new BinaryTree<string>("Mary Josephine Hannon"))
+ })
+ };
Console.WriteLine(jfkFamilyTree[PairItem.Second, PairItem.First].Value);
Console.WriteLine(jfkFamilyTree[PairItem.Second, PairItem.Second].Value);

MODIFY
src/Chapter17/Listing17.16.PlacingYieldReturnStatementsWithinALoop.cs
src/Chapter17/Listing17.16.PlacingYieldReturnStatementsWithinALoop.cs
@@ -18,7 +18,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_16
yield return Value;
// Iterate through each of the elements in the pair
- foreach(BinaryTree<T> tree in SubItems)
+ foreach(BinaryTree<T>? tree in SubItems)
{
if(tree != null)
{

MODIFY
src/Chapter17/Listing17.17.UsingForeachWithBinaryTree.cs
src/Chapter17/Listing17.17.UsingForeachWithBinaryTree.cs
@@ -10,23 +10,24 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_17
{
// JFK
var jfkFamilyTree = new BinaryTree<string>(
- "John Fitzgerald Kennedy");
-
- jfkFamilyTree.SubItems = new Pair<BinaryTree<string>>(
- new BinaryTree<string>("Joseph Patrick Kennedy"),
- new BinaryTree<string>("Rose Elizabeth Fitzgerald"));
-
+ "John Fitzgerald Kennedy")
+ {
+ SubItems = new Pair<BinaryTree<string>>(
+ new BinaryTree<string>("Joseph Patrick Kennedy")
+ {
// Grandparents (Father's side)
- jfkFamilyTree.SubItems.First.SubItems =
- new Pair<BinaryTree<string>>(
+ SubItems = new Pair<BinaryTree<string>>(
new BinaryTree<string>("Patrick Joseph Kennedy"),
- new BinaryTree<string>("Mary Augusta Hickey"));
-
+ new BinaryTree<string>("Mary Augusta Hickey"))
+ },
+ new BinaryTree<string>("Rose Elizabeth Fitzgerald")
+ {
// Grandparents (Mother's side)
- jfkFamilyTree.SubItems.Second.SubItems =
- new Pair<BinaryTree<string>>(
+ SubItems = new Pair<BinaryTree<string>>(
new BinaryTree<string>("John Francis Fitzgerald"),
- new BinaryTree<string>("Mary Josephine Hannon"));
+ new BinaryTree<string>("Mary Josephine Hannon"))
+ })
+ };
foreach (string name in jfkFamilyTree)
{

Updated Chapter 17 for nullability.