Updated for Chapter 4 Review
Moved #region INCLUDE to not include class and Main from most of the listings.
Modernized the data.
Added Constant Interpolated String example
MODIFY
src/Chapter04.Tests/Listing04.02.Negative.Tests.cs
src/Chapter04.Tests/Listing04.02.Negative.Tests.cs
@@ -8,7 +8,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_02.Tests
[TestMethod]
public void MainTest()
{
- string expected = $@"{-26457079712930.80M}";
+ string expected = $@"{-72748555131730M}";
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);

ADD
src/Chapter04.Tests/Listing04.19A.Tests.cs
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_19A.Tests
+{
+ [TestClass]
+ public class FortyTwoTests
+ {
+ [TestMethod]
+ public void Main_AdditionOperatorWithNonNumericType_StringConcatenated()
+ {
+ const string expected =
+@"The original Tacoma Bridge in Washington
+was brought down by a 42 mile/hour wind.";
+
+ IntelliTect.TestTools.Console.ConsoleAssert.Expect(
+ expected, FortyTwo.Main);
+ }
+ }
+}
\ No newline at end of file

MODIFY
src/Chapter04.Tests/Listing04.52.Tests.cs
src/Chapter04.Tests/Listing04.52.Tests.cs
@@ -9,8 +9,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_52.Tests
public void Main_EnterValidEmailWithDomain_WriteDomain()
{
const string expected =
-@"Enter an email address:
-<<First.Last@domain.com
+@"Enter an email address: <<First.Last@domain.com
>>The email domain is: domain.com";
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
@@ -21,8 +20,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_52.Tests
public void Main_EnterInvalidEmailWithoutDomain_WriteBlank()
{
const string expected =
-@"Enter an email address:
-<<First.Last
+@"Enter an email address: <<First.Last
>>The email domain is: ";
IntelliTect.TestTools.Console.ConsoleAssert.Expect(

MODIFY
src/Chapter04/Listing04.02.Negative.cs
src/Chapter04/Listing04.02.Negative.cs
@@ -5,8 +5,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_02
public static void Main()
{
#region INCLUDE
- //National Debt to the Penny
- decimal debt = -26457079712930.80M;
+ // World Debt on 2022-11-06
+ // see https://worlddebtclocks.com/
+ decimal debt = -72748555131730M;
#endregion INCLUDE
Console.WriteLine(debt);

MODIFY
src/Chapter04/Listing04.03.BinaryOperators.cs
src/Chapter04/Listing04.03.BinaryOperators.cs
#pragma warning disable CS8604 // Possible null reference argument
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_03
{
- #region INCLUDE
public class Division
{
public static void Main()
{
+ #region INCLUDE
int numerator;
int denominator;
int quotient;
@@ -24,8 +24,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_03
#endregion HIGHLIGHT
Console.WriteLine(
- $"{numerator} / {denominator} = {quotient} with remainder {remainder}");
+ $"{numerator} / {denominator} = {
+ quotient} with remainder {remainder}");
+ #endregion INCLUDE
}
}
- #endregion INCLUDE
}
\ No newline at end of file

MODIFY
src/Chapter04/Listing04.19.DeclaringConstants.cs
src/Chapter04/Listing04.19.DeclaringConstants.cs
@@ -6,14 +6,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_19
{
public class Program
{
- #region INCLUDE
public static void Main()
{
+ #region INCLUDE
const int secondsPerDay = 60 * 60 * 24;
const int secondsPerWeek = secondsPerDay * 7;
-
- // ...
- }
#endregion INCLUDE
}
}
+}

ADD
src/Chapter04/Listing04.19A.ConstantInterpolatedStrings.cs
+#pragma warning disable CS0168 // Variable is declared but never used
+#pragma warning disable CS0219 // Variable is assigned but its value is never used
+#pragma warning disable IDE0059 // Unnecessary assignment of a value
+
+namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_19A
+{
+ public class FortyTwo
+ {
+ public static void Main()
+ {
+ #region INCLUDE
+ const string windSpeed = "42";
+ const string announcement = $"""
+ The original Tacoma Bridge in Washington
+ was brought down by a { windSpeed } mile/hour wind.
+ """;
+ Console.WriteLine(announcement);
+ #endregion INCLUDE
+ }
+ }
+}

MODIFY
src/Chapter04/Listing04.20.IfElseStatement.cs
src/Chapter04/Listing04.20.IfElseStatement.cs
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_20
{
- #region INCLUDE
public class TicTacToe // Declares the TicTacToe class
{
public static void Main() // Declares the entry point of the program
{
+ #region INCLUDE
string input;
// Prompt the user to select a 1- or 2- player game
@@ -27,7 +27,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_20
Console.WriteLine(
"Play against another player.");
#endregion HIGHLIGHT
+ #endregion INCLUDE
}
}
- #endregion INCLUDE
}
\ No newline at end of file

MODIFY
src/Chapter04/Listing04.21.NestedIf.cs
src/Chapter04/Listing04.21.NestedIf.cs
#pragma warning disable CS8604 // Possible null reference argument
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_21
{
- #region INCLUDE
public class TicTacToeTrivia
{
public static void Main()
{
+ #region INCLUDE
int input; // Declare a variable to store the input
Console.Write(
@@ -39,7 +39,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_21
Console.WriteLine( // line 33
"Correct, tic-tac-toe " +
"has a maximum of 9 turns.");
+ #endregion INCLUDE
}
}
- #endregion INCLUDE
}
\ No newline at end of file

MODIFY
src/Chapter04/Listing04.24.WithCodeBlock.cs
src/Chapter04/Listing04.24.WithCodeBlock.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_24
{
- #region INCLUDE
public class CircleAreaCalculator
{
public static void Main()
{
+ #region INCLUDE
double radius; // Declare a variable to store the radius
double area; // Declare a variable to store the area
@@ -31,7 +31,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_24
Console.WriteLine(
$"{radius} is not a valid radius.");
}
+ #endregion INCLUDE
}
}
- #endregion INCLUDE
}
\ No newline at end of file

MODIFY
src/Chapter04/Listing04.27.OutOfScope.cs
src/Chapter04/Listing04.27.OutOfScope.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_27
{
- #region INCLUDE
public class Program
{
public static void Main(string[] args)
{
+ #region INCLUDE
string playerCount;
Console.Write(
"Enter the number of players (1 or 2):");
@@ -27,11 +27,15 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_27
{
// ...
}
+ #endregion INCLUDE
+ #if INCLUDE
+ #region INCLUDE
#region HIGHLIGHT
// ERROR: message is not in scope:
- // System.Console.WriteLine(message);
+ System.Console.WriteLine(message);
#endregion HIGHLIGHT
+ #endregion INCLUDE
+ #endif // INCLUDE
}
}
- #endregion INCLUDE
}
\ No newline at end of file

MODIFY
src/Chapter04/Listing04.43.WhileLoop.cs
src/Chapter04/Listing04.43.WhileLoop.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_43
{
- #region INCLUDE
public class FibonacciCalculator
{
public static void Main()
{
+ #region INCLUDE
decimal current;
decimal previous;
decimal temp;
@@ -32,7 +32,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_43
Console.WriteLine(
$"The Fibonacci number following this is { current }");
+ #endregion INCLUDE
}
}
- #endregion INCLUDE
}

MODIFY
src/Chapter04/Listing04.45.ForLoop.cs
src/Chapter04/Listing04.45.ForLoop.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_45
{
- #region INCLUDE
public class BinaryConverter
{
public static void Main()
{
- const int size = 64;
+ #region INCLUDE
+ const int size = 64; // Also available from sizeof(ulong)
ulong value;
char bit;
@@ -24,7 +24,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_45
// Shift mask one location over to the right
mask >>= 1;
}
+ #endregion INCLUDE
}
}
- #endregion INCLUDE
}

MODIFY
src/Chapter04/Listing04.47.DeterminingRemainingMoves.cs
src/Chapter04/Listing04.47.DeterminingRemainingMoves.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_47
{
- #region INCLUDE
public class TicTacToe // Declares the TicTacToe class
{
public static void Main() // Declares the entry point of the program
{
+ #region INCLUDE
// Hardcode initial board as follows
// ---+---+---
// 1 | 2 | 3
@@ -28,7 +28,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_47
Console.Write($"{ cell } ");
}
}
+ #endregion INCLUDE
}
}
- #endregion INCLUDE
}

MODIFY
src/Chapter04/Listing04.50.BreakStatement.cs
src/Chapter04/Listing04.50.BreakStatement.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_50
{
- #region INCLUDE
public class TicTacToe // Declares the TicTacToe class
{
public static void Main() // Declares the entry point of the program
{
+ #region INCLUDE
int winner = 0;
// Stores locations each player has moved
int[] playerPositions = { 0, 0 };
@@ -48,7 +48,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_50
Console.WriteLine(
$"Player { winner } was the winner");
+ #endregion INCLUDE
}
}
- #endregion INCLUDE
}
\ No newline at end of file

MODIFY
src/Chapter04/Listing04.52.DeterminingEmailDomain.cs
src/Chapter04/Listing04.52.DeterminingEmailDomain.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_52
{
- #region INCLUDE
public class EmailDomain
{
public static void Main()
{
+ #region INCLUDE
string email;
bool insideDomain = false;
- Console.WriteLine("Enter an email address: ");
+ Console.Write("Enter an email address: ");
email = Console.ReadLine() ?? string.Empty;
Console.Write("The email domain is: ");
@@ -27,7 +27,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_52
Console.Write(letter);
}
+ #endregion INCLUDE
}
}
- #endregion INCLUDE
}
\ No newline at end of file

Updated for Chapter 4 Review

Moved #region INCLUDE to not include class and Main from most of the listings.
Modernized the data.
Added Constant Interpolated String example