Code Completed for Chapter 4 Technical Review
MODIFY
src/Chapter04/Listing04.22.NestedIf.cs
src/Chapter04/Listing04.22.NestedIf.cs
@@ -18,22 +18,25 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_22
// return to an int data type
input = int.Parse(Console.ReadLine());
+ // Condition 1.
if (input <= 0) // line 16
// Input is less than or equal to 0
Console.WriteLine("Exiting...");
else
+ // Condition 2.
if (input < 9) // line 20
// Input is less than 9
Console.WriteLine(
$"Tic-tac-toe has more than {input}" +
" maximum turns.");
else
+ // Condition 3.
if (input > 9) // line 26
// Input is greater than 9
Console.WriteLine(
$"Tic-tac-toe has fewer than {input}" +
" maximum turns.");
-
+ // Condition 4.
else
// Input equals 9
Console.WriteLine( // line 33

MODIFY
src/Chapter04/Listing04.37.NullCoalescingOperator.cs
src/Chapter04/Listing04.37.NullCoalescingOperator.cs
@@ -5,7 +5,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_37
public static void Main()
{
#region INCLUDE
- string? fullName = GetDefaultDirectory();
+ string? fullName = GetSaveFilePath();
// ...
// Null-coalescing operator
@@ -22,7 +22,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_37
}
- private static string? GetDefaultDirectory()
+ private static string? GetSaveFilePath()
{
throw new NotImplementedException();
}

MODIFY
src/Chapter04/Listing04.38.NullConditionalOperator.cs
src/Chapter04/Listing04.38.NullConditionalOperator.cs
@@ -6,22 +6,26 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_38
{
#region INCLUDE
string[]? segments = null;
+ string? uri = null;
+
#region EXCLUDE
// Not shown in manuscript since args won't be null in a normal Main method.
segments = args;
- string? uri = null;
#endregion EXCLUDE
int? length = segments?.Length;
- if (length is { } && length != 0)
+ #region EXCLUDE
+ // Pattern matching also allows:
+ // the following but this is not used
+ // until covering the topic fully in Chapter 7.
+ if (length is not null and not 0) { /*...*/ }
+ #endregion EXCLUDE
+ if (length is not null && length != 0)
{
uri = string.Join('/', segments!);
}
- // Null-conditional with array accessor
- // assuming we know there is at least one element
- // uri = segments?[0];
if (uri is null || length is 0)
{
Console.WriteLine(

MODIFY
src/Chapter04/Table04.04.CheckForNull.cs
src/Chapter04/Table04.04.CheckForNull.cs
@@ -2,44 +2,98 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Table04_04
{
public class Program
{
- public static void EqualityCheckForNull(string? uri = null)
+ public static void PatternMatchingIsNull(string? uri = null!)
{
- if (uri != null)
+ // 1.
+ if (uri is null)
+ {
+ Console.WriteLine(
+ "Uri is null");
+ }
+ else
+ {
+ Console.WriteLine(
+ $"Uri is: {uri}");
+ }
+ }
+ public static void PatternMatchingIsNotNull(string? uri = null)
+ {
+ // 2.
+ if (uri is not null)
{
Console.WriteLine(
$"Uri is: {uri}");
}
- else // (uri == null)
+ else
{
Console.WriteLine(
"Uri is null");
}
}
- public static void PatternMatchingCheckForNull(string? uri = null)
+ public static void EqualityInEqualityCheckForNull(string? uri = null)
+ {
+ // 3.
+ if (uri == null)
{
+ Console.WriteLine(
+ "Uri is null");
+ }
+
if (uri != null)
{
Console.WriteLine(
$"Uri is: {uri}");
}
- else // (uri == null)
+
+ }
+ public static void IsObject(string? uri = null!)
+ {
+ // 4.
+ int number = 0;
+ if ((uri is object)
+ // Warning CS0183: The given
+ // expression is always not
+ // null.
+ && (number is object)
+ )
+ {
+ Console.WriteLine(
+ $"Uri is: {uri}");
+ }
+ else
{
Console.WriteLine(
"Uri is null");
}
}
- public static void PropertyPatternMatchingCheckForNull(string? uri = null)
+ public static void IsPatternMatching(string? uri = null!)
{
- if (uri != null)
+ // 5.
+ if (uri is { })
{
Console.WriteLine(
$"Uri is: {uri}");
}
- else // (uri == null)
+ else
+ {
+ Console.WriteLine(
+ "Uri is null");
+ }
+ }
+ public static void ReferenceEqualsCheckForNotNull(string? uri = null)
+ {
+ // 6.
+ if (ReferenceEquals(
+ uri, null))
{
Console.WriteLine(
"Uri is null");
}
+ else
+ {
+ Console.WriteLine(
+ $"Uri is: {uri}");
+ }
}
}
}

Code Completed for Chapter 4 Technical Review (#397)

Co-authored-by: Benjamin Michaelis <benjamin@michaelis.net>
Co-authored-by: Kevin B <Keboo@users.noreply.github.com>