From e5cb90928b3a3d56522cda4e3bf0c137cb35931d Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Fri, 8 Apr 2022 18:29:35 -0700 Subject: [PATCH 1/6] Add Regions to Chapter 3 --- src/Chapter03/Chapter03.csproj | 5 ++--- src/Chapter03/Listing03.01.CheckingForNull.cs | 10 +++++----- .../Listing03.02.DereferencingAnUnassignedVariable.cs | 6 ++++-- ...3.03.EnablingNullableProjectWide-PlaceHolderFile.cs | 10 ---------- ...xml => Listing03.03.EnablingNullableProjectWide.cs} | 6 ++++-- src/Chapter03/Listing03.04.WorkingWithStrings.cs | 2 ++ ...ng03.05.ImplicitLocalVariablesWithAnonymousTypes.cs | 2 ++ ...ntOfCompilerGeneratedCILCodeForAValueTupleReturn.cs | 9 +++------ src/Chapter03/Listing03.07.DeclaringAnArray.cs | 2 ++ .../Listing03.08.DeclaringATwoDimensionalArray.cs | 2 ++ .../Listing03.09.ArrayDeclarationWithAssignment.cs | 2 ++ ...Listing03.10.ArrayAssignmentFollowingDeclaration.cs | 2 ++ ...ing03.11.ArrayAssignmentWithNewDuringDeclaration.cs | 2 ++ ...g03.12.DeclarationAndAssignmentWithTheNewKeyword.cs | 2 ++ .../Listing03.13.AssigningWithoutLiteralValues.cs | 2 ++ .../Listing03.14.DefiningTheArraySizeAtRuntime.cs | 2 ++ .../Listing03.15.DeclaringATwoDimensionalArray.cs | 2 ++ ...03.16.InitializingATwoDimensionalArrayOfIntegers.cs | 2 ++ ...mensionalArrayWithInconsistentSizeCausingAnError.cs | 2 ++ .../Listing03.18.InitializingAThreeDimensionalArray.cs | 2 ++ src/Chapter03/Listing03.19.InitializingAJaggedArray.cs | 2 ++ .../Listing03.20.DeclaringAndAccessingAnArray.cs | 2 ++ ...sting03.21.SwappingDataBetweenPositionsInAnArray.cs | 2 ++ ...03.22.InitializingATwoDimensionalArrayOfIntegers.cs | 2 ++ src/Chapter03/Listing03.23.DeclaringAJaggedArray.cs | 2 ++ .../Listing03.24.RetrievingTheLengthOfAnArray.cs | 2 ++ ...singOutsideTheBoundsOfAnArrayThrowingAnException.cs | 2 ++ .../Listing03.26.UsingLengthMinus1InTheArrayIndex.cs | 2 ++ src/Chapter03/Listing03.27.Slicing.cs | 2 ++ src/Chapter03/Listing03.28.AdditionalArrayMethods.cs | 10 ++++++++++ ...Listing03.29.RetrievingAParticularDimensionsSize.cs | 6 ++++-- .../Listing03.30.LookingForCommandlineOptions.cs | 2 ++ ...ting03.31.LookingForCommandlineOptionsSimplified.cs | 2 ++ src/Chapter03/Listing03.32.ReversingAString.cs | 6 ++++++ 34 files changed, 88 insertions(+), 30 deletions(-) delete mode 100644 src/Chapter03/Listing03.03.EnablingNullableProjectWide-PlaceHolderFile.cs rename src/Chapter03/{Listing03.03.EnablingNullableProjectWide.csproj.xml => Listing03.03.EnablingNullableProjectWide.cs} (51%) diff --git a/src/Chapter03/Chapter03.csproj b/src/Chapter03/Chapter03.csproj index 5ceff6f9b..c50dd10f2 100644 --- a/src/Chapter03/Chapter03.csproj +++ b/src/Chapter03/Chapter03.csproj @@ -13,7 +13,7 @@ - + @@ -25,8 +25,7 @@ PreserveNewest - - + diff --git a/src/Chapter03/Listing03.01.CheckingForNull.cs b/src/Chapter03/Listing03.01.CheckingForNull.cs index bf1c30580..2827bdf5f 100644 --- a/src/Chapter03/Listing03.01.CheckingForNull.cs +++ b/src/Chapter03/Listing03.01.CheckingForNull.cs @@ -2,16 +2,16 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_01 { public class Program { + #region INCLUDE public static void Main(string[] args) { int? number = null; - - // ... - if(args.Length>0) + #region EXCLUDE + if (args.Length>0) { number = args[0].Length; } - + #endregion if (number is null) { System.Console.WriteLine( @@ -22,8 +22,8 @@ public static void Main(string[] args) System.Console.WriteLine( $"'number' doubled is { number * 2 }."); } - } + #endregion } } diff --git a/src/Chapter03/Listing03.02.DereferencingAnUnassignedVariable.cs b/src/Chapter03/Listing03.02.DereferencingAnUnassignedVariable.cs index 93f8dcf73..ad6884044 100644 --- a/src/Chapter03/Listing03.02.DereferencingAnUnassignedVariable.cs +++ b/src/Chapter03/Listing03.02.DereferencingAnUnassignedVariable.cs @@ -2,13 +2,15 @@ //{ public class Program { - public static void Main() + #region INCLUDE + #nullable enable + public static void Main() { string? text; // ... - // Compile Error: Use of unassigned local variable 'text' System.Console.WriteLine(text.Length); } } + #endregion //} diff --git a/src/Chapter03/Listing03.03.EnablingNullableProjectWide-PlaceHolderFile.cs b/src/Chapter03/Listing03.03.EnablingNullableProjectWide-PlaceHolderFile.cs deleted file mode 100644 index d09be3d9d..000000000 --- a/src/Chapter03/Listing03.03.EnablingNullableProjectWide-PlaceHolderFile.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_03 -{ - class Listing03 - { - } -} diff --git a/src/Chapter03/Listing03.03.EnablingNullableProjectWide.csproj.xml b/src/Chapter03/Listing03.03.EnablingNullableProjectWide.cs similarity index 51% rename from src/Chapter03/Listing03.03.EnablingNullableProjectWide.csproj.xml rename to src/Chapter03/Listing03.03.EnablingNullableProjectWide.cs index 36c64642f..f3e7a9f8a 100644 --- a/src/Chapter03/Listing03.03.EnablingNullableProjectWide.csproj.xml +++ b/src/Chapter03/Listing03.03.EnablingNullableProjectWide.cs @@ -1,7 +1,9 @@ - +#region INCLUDE +< Project Sdk="Microsoft.NET.Sdk"> Exe - net5.0 + net6.0 enable +#endregion diff --git a/src/Chapter03/Listing03.04.WorkingWithStrings.cs b/src/Chapter03/Listing03.04.WorkingWithStrings.cs index 53c56c5d3..458f7aab6 100644 --- a/src/Chapter03/Listing03.04.WorkingWithStrings.cs +++ b/src/Chapter03/Listing03.04.WorkingWithStrings.cs @@ -1,5 +1,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_04 { + #region INCLUDE public class Uppercase { public static void Main() @@ -13,4 +14,5 @@ public static void Main() System.Console.WriteLine(uppercase); } } + #endregion } diff --git a/src/Chapter03/Listing03.05.ImplicitLocalVariablesWithAnonymousTypes.cs b/src/Chapter03/Listing03.05.ImplicitLocalVariablesWithAnonymousTypes.cs index 448b62bdc..6985df702 100644 --- a/src/Chapter03/Listing03.05.ImplicitLocalVariablesWithAnonymousTypes.cs +++ b/src/Chapter03/Listing03.05.ImplicitLocalVariablesWithAnonymousTypes.cs @@ -1,5 +1,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_05 { + #region INCLUDE public class Program { public static void Main() @@ -17,4 +18,5 @@ public static void Main() $"{patent2.Title} ({patent2.YearOfPublication})"); } } + #endregion } diff --git a/src/Chapter03/Listing03.06.TheCSharpEquivalentOfCompilerGeneratedCILCodeForAValueTupleReturn.cs b/src/Chapter03/Listing03.06.TheCSharpEquivalentOfCompilerGeneratedCILCodeForAValueTupleReturn.cs index 96a65e8f6..0f4683579 100644 --- a/src/Chapter03/Listing03.06.TheCSharpEquivalentOfCompilerGeneratedCILCodeForAValueTupleReturn.cs +++ b/src/Chapter03/Listing03.06.TheCSharpEquivalentOfCompilerGeneratedCILCodeForAValueTupleReturn.cs @@ -1,13 +1,10 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_06 { - namespace Chapter03 - { - [return: System.Runtime.CompilerServices.TupleElementNames(new string[] { "First", "Second" })] + #region INCLUDE + [return: System.Runtime.CompilerServices.TupleElementNames(new string[] { "First", "Second" })] public System.ValueTuple ParseNames(string fullName) { // ... } - - } - + #endregion } diff --git a/src/Chapter03/Listing03.07.DeclaringAnArray.cs b/src/Chapter03/Listing03.07.DeclaringAnArray.cs index 17a36ed26..46f005eb7 100644 --- a/src/Chapter03/Listing03.07.DeclaringAnArray.cs +++ b/src/Chapter03/Listing03.07.DeclaringAnArray.cs @@ -5,7 +5,9 @@ public class Program { public static void Main() { + #region INCLUDE string[] languages; + #endregion } } } diff --git a/src/Chapter03/Listing03.08.DeclaringATwoDimensionalArray.cs b/src/Chapter03/Listing03.08.DeclaringATwoDimensionalArray.cs index 8c631826f..d5540f5a7 100644 --- a/src/Chapter03/Listing03.08.DeclaringATwoDimensionalArray.cs +++ b/src/Chapter03/Listing03.08.DeclaringATwoDimensionalArray.cs @@ -5,12 +5,14 @@ public class Program { public static void Main() { + #region INCLUDE // | | // ---+---+--- // | | // ---+---+--- // | | int[,] cells; + #endregion } } } diff --git a/src/Chapter03/Listing03.09.ArrayDeclarationWithAssignment.cs b/src/Chapter03/Listing03.09.ArrayDeclarationWithAssignment.cs index 4ac76c1e1..ebff516aa 100644 --- a/src/Chapter03/Listing03.09.ArrayDeclarationWithAssignment.cs +++ b/src/Chapter03/Listing03.09.ArrayDeclarationWithAssignment.cs @@ -4,9 +4,11 @@ public class Program { public static void Main() { + #region INCLUDE string[] languages = { "C#", "COBOL", "Java", "C++", "TypeScript", "Visual Basic", "Python", "Lisp", "JavaScript" }; + #endregion } } } diff --git a/src/Chapter03/Listing03.10.ArrayAssignmentFollowingDeclaration.cs b/src/Chapter03/Listing03.10.ArrayAssignmentFollowingDeclaration.cs index a71e9fefa..627b2a0d7 100644 --- a/src/Chapter03/Listing03.10.ArrayAssignmentFollowingDeclaration.cs +++ b/src/Chapter03/Listing03.10.ArrayAssignmentFollowingDeclaration.cs @@ -4,10 +4,12 @@ public class Program { public static void Main() { + #region INCLUDE string[] languages; languages = new string[] { "C#", "COBOL", "Java", "C++", "TypeScript", "Visual Basic", "Python", "Lisp", "JavaScript" }; + #endregion } } } diff --git a/src/Chapter03/Listing03.11.ArrayAssignmentWithNewDuringDeclaration.cs b/src/Chapter03/Listing03.11.ArrayAssignmentWithNewDuringDeclaration.cs index 733929cba..45c252fee 100644 --- a/src/Chapter03/Listing03.11.ArrayAssignmentWithNewDuringDeclaration.cs +++ b/src/Chapter03/Listing03.11.ArrayAssignmentWithNewDuringDeclaration.cs @@ -4,10 +4,12 @@ public class Program { public static void Main() { + #region INCLUDE string[] languages = new string[] { "C#", "COBOL", "Java", "C++", "TypeScript", "Visual Basic", "Python", "Lisp", "JavaScript" }; + #endregion } } } diff --git a/src/Chapter03/Listing03.12.DeclarationAndAssignmentWithTheNewKeyword.cs b/src/Chapter03/Listing03.12.DeclarationAndAssignmentWithTheNewKeyword.cs index d9cad152e..e74e67bf5 100644 --- a/src/Chapter03/Listing03.12.DeclarationAndAssignmentWithTheNewKeyword.cs +++ b/src/Chapter03/Listing03.12.DeclarationAndAssignmentWithTheNewKeyword.cs @@ -4,10 +4,12 @@ public class Program { public static void Main() { + #region INCLUDE string[] languages = new string[9] { "C#", "COBOL", "Java", "C++", "TypeScript", "Visual Basic", "Python", "Lisp", "JavaScript" }; + #endregion } } } diff --git a/src/Chapter03/Listing03.13.AssigningWithoutLiteralValues.cs b/src/Chapter03/Listing03.13.AssigningWithoutLiteralValues.cs index 83cf482e0..9e3f45c52 100644 --- a/src/Chapter03/Listing03.13.AssigningWithoutLiteralValues.cs +++ b/src/Chapter03/Listing03.13.AssigningWithoutLiteralValues.cs @@ -4,7 +4,9 @@ public class Program { public static void Main() { + #region INCLUDE string[] languages = new string[9]; + #endregion } } } diff --git a/src/Chapter03/Listing03.14.DefiningTheArraySizeAtRuntime.cs b/src/Chapter03/Listing03.14.DefiningTheArraySizeAtRuntime.cs index 3dfd8aade..7d7289f22 100644 --- a/src/Chapter03/Listing03.14.DefiningTheArraySizeAtRuntime.cs +++ b/src/Chapter03/Listing03.14.DefiningTheArraySizeAtRuntime.cs @@ -4,11 +4,13 @@ public class Program { public static void Main() { + #region INCLUDE string[] groceryList; System.Console.Write("How many items on the list? "); int size = int.Parse(System.Console.ReadLine()); groceryList = new string[size]; // ... + #endregion } } } diff --git a/src/Chapter03/Listing03.15.DeclaringATwoDimensionalArray.cs b/src/Chapter03/Listing03.15.DeclaringATwoDimensionalArray.cs index 342eeafe7..41848a5a7 100644 --- a/src/Chapter03/Listing03.15.DeclaringATwoDimensionalArray.cs +++ b/src/Chapter03/Listing03.15.DeclaringATwoDimensionalArray.cs @@ -4,7 +4,9 @@ public class Program { public static void Main() { + #region INCLUDE int[,] cells = new int[3,3]; + #endregion } } } diff --git a/src/Chapter03/Listing03.16.InitializingATwoDimensionalArrayOfIntegers.cs b/src/Chapter03/Listing03.16.InitializingATwoDimensionalArrayOfIntegers.cs index 6c9180ba1..80f11ba22 100644 --- a/src/Chapter03/Listing03.16.InitializingATwoDimensionalArrayOfIntegers.cs +++ b/src/Chapter03/Listing03.16.InitializingATwoDimensionalArrayOfIntegers.cs @@ -4,11 +4,13 @@ public class Program { public static void Main() { + #region INCLUDE int[,] cells = { {1, 0, 2}, {1, 2, 0}, {1, 2, 1} }; + #endregion } } } diff --git a/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs b/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs index 61930df64..478187f37 100644 --- a/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs +++ b/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs @@ -4,6 +4,7 @@ public class Program { public static void Main() { + #region INCLUDE // ERROR: Each dimension must be consistently sized /* int[,] cells = { @@ -13,6 +14,7 @@ public static void Main() {1} }; */ + #endregion } } } diff --git a/src/Chapter03/Listing03.18.InitializingAThreeDimensionalArray.cs b/src/Chapter03/Listing03.18.InitializingAThreeDimensionalArray.cs index e57e24dec..16b09e5fe 100644 --- a/src/Chapter03/Listing03.18.InitializingAThreeDimensionalArray.cs +++ b/src/Chapter03/Listing03.18.InitializingAThreeDimensionalArray.cs @@ -4,6 +4,7 @@ public class Program { public static void Main() { + #region INCLUDE bool[, ,] cells; cells = new bool[2, 3, 3] { @@ -20,6 +21,7 @@ public static void Main() {false, true, true} // ---+---+--- } // | O | }; + #endregion } } } diff --git a/src/Chapter03/Listing03.19.InitializingAJaggedArray.cs b/src/Chapter03/Listing03.19.InitializingAJaggedArray.cs index f925cf048..6a47f73ea 100644 --- a/src/Chapter03/Listing03.19.InitializingAJaggedArray.cs +++ b/src/Chapter03/Listing03.19.InitializingAJaggedArray.cs @@ -4,12 +4,14 @@ public class Program { public static void Main() { + #region INCLUDE int[][] cells = { new int[] {1, 0, 2, 0}, new int[] {1, 2, 0}, new int[] {1, 2}, new int[] {1} }; + #endregion } } } diff --git a/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs b/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs index e94f61a1f..508731a53 100644 --- a/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs +++ b/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs @@ -4,6 +4,7 @@ public class Program { public static void Main() { + #region INCLUDE string[] languages = new string[] { "C#", "COBOL", "Java", "C++", "TypeScript", "Visual Basic", @@ -16,6 +17,7 @@ public static void Main() language = languages[^3]; // Write �Python� System.Console.WriteLine(language); + #endregion } } } diff --git a/src/Chapter03/Listing03.21.SwappingDataBetweenPositionsInAnArray.cs b/src/Chapter03/Listing03.21.SwappingDataBetweenPositionsInAnArray.cs index 32bf9ae37..136592208 100644 --- a/src/Chapter03/Listing03.21.SwappingDataBetweenPositionsInAnArray.cs +++ b/src/Chapter03/Listing03.21.SwappingDataBetweenPositionsInAnArray.cs @@ -4,6 +4,7 @@ public class Program { public static void Main() { + #region INCLUDE string[] languages = new string[] { "C#", "COBOL", "Java", "C++", "TypeScript", "Visual Basic", @@ -14,6 +15,7 @@ public static void Main() languages[3] = languages[2]; // Assign language to location of "Java" languages[2] = language; + #endregion } } } diff --git a/src/Chapter03/Listing03.22.InitializingATwoDimensionalArrayOfIntegers.cs b/src/Chapter03/Listing03.22.InitializingATwoDimensionalArrayOfIntegers.cs index 548514bd5..15e4731ac 100644 --- a/src/Chapter03/Listing03.22.InitializingATwoDimensionalArrayOfIntegers.cs +++ b/src/Chapter03/Listing03.22.InitializingATwoDimensionalArrayOfIntegers.cs @@ -4,6 +4,7 @@ public class Program { public static void Main() { + #region INCLUDE int[,] cells = { {1, 0, 2}, {0, 2, 0}, @@ -11,6 +12,7 @@ public static void Main() }; // Set the winning tic-tac-toe move to be player 1 cells[1, 0] = 1; + #endregion } } } diff --git a/src/Chapter03/Listing03.23.DeclaringAJaggedArray.cs b/src/Chapter03/Listing03.23.DeclaringAJaggedArray.cs index 9a1b4aa20..677af0662 100644 --- a/src/Chapter03/Listing03.23.DeclaringAJaggedArray.cs +++ b/src/Chapter03/Listing03.23.DeclaringAJaggedArray.cs @@ -4,6 +4,7 @@ public class Program { public static void Main() { + #region INCLUDE int[][] cells = { new int[] {1, 0, 2}, new int[] {0, 2, 0}, @@ -12,6 +13,7 @@ public static void Main() cells[1][0] = 1; // ... + #endregion } } } diff --git a/src/Chapter03/Listing03.24.RetrievingTheLengthOfAnArray.cs b/src/Chapter03/Listing03.24.RetrievingTheLengthOfAnArray.cs index 6fdc4830a..ae43764f2 100644 --- a/src/Chapter03/Listing03.24.RetrievingTheLengthOfAnArray.cs +++ b/src/Chapter03/Listing03.24.RetrievingTheLengthOfAnArray.cs @@ -6,8 +6,10 @@ public static void Main() { string[] languages = new string[9]; // ... + #region INCLUDE System.Console.WriteLine( $"There are {languages.Length} languages in the array."); + #endregion } } } diff --git a/src/Chapter03/Listing03.25.AccessingOutsideTheBoundsOfAnArrayThrowingAnException.cs b/src/Chapter03/Listing03.25.AccessingOutsideTheBoundsOfAnArrayThrowingAnException.cs index 90f3b22f9..ebd816732 100644 --- a/src/Chapter03/Listing03.25.AccessingOutsideTheBoundsOfAnArrayThrowingAnException.cs +++ b/src/Chapter03/Listing03.25.AccessingOutsideTheBoundsOfAnArrayThrowingAnException.cs @@ -4,11 +4,13 @@ public class Program { public static void Main() { + #region INCLUDE string[] languages = new string[9]; // ... // RUNTIME ERROR: index out of bounds - should // be 8 for the last element languages[4] = languages[9]; + #endregion } } } diff --git a/src/Chapter03/Listing03.26.UsingLengthMinus1InTheArrayIndex.cs b/src/Chapter03/Listing03.26.UsingLengthMinus1InTheArrayIndex.cs index 7eee5b945..dd3662412 100644 --- a/src/Chapter03/Listing03.26.UsingLengthMinus1InTheArrayIndex.cs +++ b/src/Chapter03/Listing03.26.UsingLengthMinus1InTheArrayIndex.cs @@ -6,9 +6,11 @@ public class Program { public static void Main() { + #region INCLUDE string[] languages = new string[9]; // ... languages[4] = languages[languages.Length - 1]; + #endregion } } } diff --git a/src/Chapter03/Listing03.27.Slicing.cs b/src/Chapter03/Listing03.27.Slicing.cs index 4b912987b..f53d49f99 100644 --- a/src/Chapter03/Listing03.27.Slicing.cs +++ b/src/Chapter03/Listing03.27.Slicing.cs @@ -4,6 +4,7 @@ public class Program { public static void Main() { + #region INCLUDE string[] languages = new string[] { "C#", "COBOL", "Java", "C++", "TypeScript", "Swift", @@ -32,6 +33,7 @@ public static void Main() // C#, COBOL, Java, C++, TypeScript, Swift, Python, Lisp, JavaScript string.Join(", ", languages[0..^0]) // Python, Lisp, JavaScript }"); + #endregion } } } diff --git a/src/Chapter03/Listing03.28.AdditionalArrayMethods.cs b/src/Chapter03/Listing03.28.AdditionalArrayMethods.cs index 31390a1f7..758673ca2 100644 --- a/src/Chapter03/Listing03.28.AdditionalArrayMethods.cs +++ b/src/Chapter03/Listing03.28.AdditionalArrayMethods.cs @@ -1,5 +1,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_28 { + #region INCLUDE public class ProgrammingLanguages { public static void Main() @@ -9,11 +10,15 @@ public static void Main() "C++", "TypeScript", "Swift", "Python", "Lisp", "JavaScript"}; + #region HIGHLIGHT System.Array.Sort(languages); + #endregion string searchString = "COBOL"; + #region HIGHLIGHT int index = System.Array.BinarySearch( languages, searchString); + #endregion System.Console.WriteLine( "The wave of the future, " + $"{ searchString }, is at index { index }."); @@ -25,17 +30,22 @@ public static void Main() $"{ "-------------",-20 }\t{ "------------",-20 }"); System.Console.WriteLine( $"{ languages[0],-20 }\t{ languages[^1],-20 }"); + #region INCLUDE System.Array.Reverse(languages); + #endregion System.Console.WriteLine( $"{ languages[0],-20 }\t{ languages[^1],-20 }"); // Note this does not remove all items from the array // Rather it sets each item to the type's default value + #region INCLUDE System.Array.Clear(languages, 0, languages.Length); + #endregion System.Console.WriteLine( $"{ languages[0],-20 }\t{ languages[^1],-20 }"); System.Console.WriteLine( $"After clearing, the array size is: { languages.Length }"); } } + #endregion } diff --git a/src/Chapter03/Listing03.29.RetrievingAParticularDimensionsSize.cs b/src/Chapter03/Listing03.29.RetrievingAParticularDimensionsSize.cs index d54e582e2..9bcc66807 100644 --- a/src/Chapter03/Listing03.29.RetrievingAParticularDimensionsSize.cs +++ b/src/Chapter03/Listing03.29.RetrievingAParticularDimensionsSize.cs @@ -4,10 +4,12 @@ public class Program { public static void Main() { + #region INCLUDE bool[, ,] cells; cells = new bool[2, 3, 3]; - System.Console.WriteLine(cells.GetLength(0)); // Displays 2 - System.Console.WriteLine(cells.Rank); // Displays 3 + System.Console.WriteLine(cells.GetLength(0)); // Displays 2 + System.Console.WriteLine(cells.Rank); // Displays 3 + #endregion } } } diff --git a/src/Chapter03/Listing03.30.LookingForCommandlineOptions.cs b/src/Chapter03/Listing03.30.LookingForCommandlineOptions.cs index 0d24a4bdc..8e0ab3d59 100644 --- a/src/Chapter03/Listing03.30.LookingForCommandlineOptions.cs +++ b/src/Chapter03/Listing03.30.LookingForCommandlineOptions.cs @@ -2,6 +2,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_30 { public class Program { + #region INCLUDE public static void Main(string[] args) { // ... @@ -10,5 +11,6 @@ public static void Main(string[] args) // This parameter is an option } } + #endregion } } diff --git a/src/Chapter03/Listing03.31.LookingForCommandlineOptionsSimplified.cs b/src/Chapter03/Listing03.31.LookingForCommandlineOptionsSimplified.cs index aedaf51aa..cbd168f97 100644 --- a/src/Chapter03/Listing03.31.LookingForCommandlineOptionsSimplified.cs +++ b/src/Chapter03/Listing03.31.LookingForCommandlineOptionsSimplified.cs @@ -2,6 +2,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_31 { public class Program { + #region INCLUDE public static void Main(string[] args) { // ... @@ -11,5 +12,6 @@ public static void Main(string[] args) // This parameter is an option } } + #endregion } } diff --git a/src/Chapter03/Listing03.32.ReversingAString.cs b/src/Chapter03/Listing03.32.ReversingAString.cs index 2db5fd2ac..d6a41d5b9 100644 --- a/src/Chapter03/Listing03.32.ReversingAString.cs +++ b/src/Chapter03/Listing03.32.ReversingAString.cs @@ -1,5 +1,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_32 { + #region INCLUDE public class Palindrome { public static void Main() @@ -14,11 +15,15 @@ public static void Main() reverse = palindrome.Replace(" ", ""); reverse = reverse.ToLower(); + #region HIGHLIGHT // Convert to an array temp = reverse.ToCharArray(); + #endregion + #region INCLUDE // Reverse the array System.Array.Reverse(temp); + #endregion // Convert the array back to a string and // check if reverse string is the same @@ -34,4 +39,5 @@ public static void Main() } } } + #endregion } From d18c963157ef876fda5d22e8200fd677fffdba83 Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Fri, 8 Apr 2022 18:55:02 -0700 Subject: [PATCH 2/6] Minor Updates --- src/Chapter03/Listing03.01.CheckingForNull.cs | 2 +- ...lerGeneratedCILCodeForAValueTupleReturn.cs | 8 ++--- ....10.ArrayAssignmentFollowingDeclaration.cs | 2 +- ...ArrayAssignmentWithNewDuringDeclaration.cs | 2 +- ...clarationAndAssignmentWithTheNewKeyword.cs | 2 +- ...ArrayWithInconsistentSizeCausingAnError.cs | 2 ++ ...3.18.InitializingAThreeDimensionalArray.cs | 36 ++++++++++++------- ...sting03.20.DeclaringAndAccessingAnArray.cs | 2 +- .../Listing03.23.DeclaringAJaggedArray.cs | 6 ++-- .../Listing03.28.AdditionalArrayMethods.cs | 4 +-- .../Listing03.32.ReversingAString.cs | 2 +- 11 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/Chapter03/Listing03.01.CheckingForNull.cs b/src/Chapter03/Listing03.01.CheckingForNull.cs index 2827bdf5f..c3a7e0e4a 100644 --- a/src/Chapter03/Listing03.01.CheckingForNull.cs +++ b/src/Chapter03/Listing03.01.CheckingForNull.cs @@ -3,7 +3,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_01 public class Program { #region INCLUDE - public static void Main(string[] args) + public static void Main() { int? number = null; #region EXCLUDE diff --git a/src/Chapter03/Listing03.06.TheCSharpEquivalentOfCompilerGeneratedCILCodeForAValueTupleReturn.cs b/src/Chapter03/Listing03.06.TheCSharpEquivalentOfCompilerGeneratedCILCodeForAValueTupleReturn.cs index 0f4683579..7505a98f2 100644 --- a/src/Chapter03/Listing03.06.TheCSharpEquivalentOfCompilerGeneratedCILCodeForAValueTupleReturn.cs +++ b/src/Chapter03/Listing03.06.TheCSharpEquivalentOfCompilerGeneratedCILCodeForAValueTupleReturn.cs @@ -2,9 +2,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_06 { #region INCLUDE [return: System.Runtime.CompilerServices.TupleElementNames(new string[] { "First", "Second" })] - public System.ValueTuple ParseNames(string fullName) - { - // ... - } + public System.ValueTuple ParseNames(string fullName) + { + // ... + } #endregion } diff --git a/src/Chapter03/Listing03.10.ArrayAssignmentFollowingDeclaration.cs b/src/Chapter03/Listing03.10.ArrayAssignmentFollowingDeclaration.cs index 627b2a0d7..b8afe7c3a 100644 --- a/src/Chapter03/Listing03.10.ArrayAssignmentFollowingDeclaration.cs +++ b/src/Chapter03/Listing03.10.ArrayAssignmentFollowingDeclaration.cs @@ -6,7 +6,7 @@ public static void Main() { #region INCLUDE string[] languages; - languages = new string[] { "C#", "COBOL", "Java", + languages = new string[]{ "C#", "COBOL", "Java", "C++", "TypeScript", "Visual Basic", "Python", "Lisp", "JavaScript" }; #endregion diff --git a/src/Chapter03/Listing03.11.ArrayAssignmentWithNewDuringDeclaration.cs b/src/Chapter03/Listing03.11.ArrayAssignmentWithNewDuringDeclaration.cs index 45c252fee..14f3cd938 100644 --- a/src/Chapter03/Listing03.11.ArrayAssignmentWithNewDuringDeclaration.cs +++ b/src/Chapter03/Listing03.11.ArrayAssignmentWithNewDuringDeclaration.cs @@ -5,7 +5,7 @@ public class Program public static void Main() { #region INCLUDE - string[] languages = new string[] { + string[] languages = new string[]{ "C#", "COBOL", "Java", "C++", "TypeScript", "Visual Basic", "Python", "Lisp", "JavaScript" }; diff --git a/src/Chapter03/Listing03.12.DeclarationAndAssignmentWithTheNewKeyword.cs b/src/Chapter03/Listing03.12.DeclarationAndAssignmentWithTheNewKeyword.cs index e74e67bf5..b41b2ea01 100644 --- a/src/Chapter03/Listing03.12.DeclarationAndAssignmentWithTheNewKeyword.cs +++ b/src/Chapter03/Listing03.12.DeclarationAndAssignmentWithTheNewKeyword.cs @@ -5,7 +5,7 @@ public class Program public static void Main() { #region INCLUDE - string[] languages = new string[9] { + string[] languages = new string[9]{ "C#", "COBOL", "Java", "C++", "TypeScript", "Visual Basic", "Python", "Lisp", "JavaScript" }; diff --git a/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs b/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs index 478187f37..8cf552374 100644 --- a/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs +++ b/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs @@ -7,12 +7,14 @@ public static void Main() #region INCLUDE // ERROR: Each dimension must be consistently sized /* + #region EXCLUDE int[,] cells = { {1, 0, 2, 0}, {1, 2, 0}, {1, 2}, {1} }; + #endregion */ #endregion } diff --git a/src/Chapter03/Listing03.18.InitializingAThreeDimensionalArray.cs b/src/Chapter03/Listing03.18.InitializingAThreeDimensionalArray.cs index 16b09e5fe..0f744ed4c 100644 --- a/src/Chapter03/Listing03.18.InitializingAThreeDimensionalArray.cs +++ b/src/Chapter03/Listing03.18.InitializingAThreeDimensionalArray.cs @@ -6,20 +6,30 @@ public static void Main() { #region INCLUDE bool[, ,] cells; - cells = new bool[2, 3, 3] + cells = new bool[2, 3, 3] { - // Player 1 moves - { // X | | - {true, false, false}, // ---+---+--- - {true, false, false}, // X | | - {true, false, true} // ---+---+--- - }, // X | | X - // Player 2 moves - { // | | O - {false, false, true}, // ---+---+--- - {false, true, false}, // | O | - {false, true, true} // ---+---+--- - } // | O | + // Player 1 moves + // X | | + // ---+---+--- + // X | | + // ---+---+--- + // X | | X + { + {true, false, false}, + {true, false, false}, + {true, false, true} + }, + // Player 2 moves + // | | O + // ---+---+--- + // | O | + // ---+---+--- + // | O | + { + {false, false, true}, + {false, true, false}, + {false, true, true} + } }; #endregion } diff --git a/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs b/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs index 508731a53..e6217a25a 100644 --- a/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs +++ b/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs @@ -5,7 +5,7 @@ public class Program public static void Main() { #region INCLUDE - string[] languages = new string[] { + string[] languages = new string[9]{ "C#", "COBOL", "Java", "C++", "TypeScript", "Visual Basic", "Python", "Lisp", "JavaScript"}; diff --git a/src/Chapter03/Listing03.23.DeclaringAJaggedArray.cs b/src/Chapter03/Listing03.23.DeclaringAJaggedArray.cs index 677af0662..67249d0f2 100644 --- a/src/Chapter03/Listing03.23.DeclaringAJaggedArray.cs +++ b/src/Chapter03/Listing03.23.DeclaringAJaggedArray.cs @@ -6,9 +6,9 @@ public static void Main() { #region INCLUDE int[][] cells = { - new int[] {1, 0, 2}, - new int[] {0, 2, 0}, - new int[] {1, 2, 1} + new int[]{1, 0, 2}, + new int[]{0, 2, 0}, + new int[]{1, 2, 1} }; cells[1][0] = 1; diff --git a/src/Chapter03/Listing03.28.AdditionalArrayMethods.cs b/src/Chapter03/Listing03.28.AdditionalArrayMethods.cs index 758673ca2..b98037d33 100644 --- a/src/Chapter03/Listing03.28.AdditionalArrayMethods.cs +++ b/src/Chapter03/Listing03.28.AdditionalArrayMethods.cs @@ -30,14 +30,14 @@ public static void Main() $"{ "-------------",-20 }\t{ "------------",-20 }"); System.Console.WriteLine( $"{ languages[0],-20 }\t{ languages[^1],-20 }"); - #region INCLUDE + #region HIGHLIGHT System.Array.Reverse(languages); #endregion System.Console.WriteLine( $"{ languages[0],-20 }\t{ languages[^1],-20 }"); // Note this does not remove all items from the array // Rather it sets each item to the type's default value - #region INCLUDE + #region HIGHLIGHT System.Array.Clear(languages, 0, languages.Length); #endregion System.Console.WriteLine( diff --git a/src/Chapter03/Listing03.32.ReversingAString.cs b/src/Chapter03/Listing03.32.ReversingAString.cs index d6a41d5b9..3198974bb 100644 --- a/src/Chapter03/Listing03.32.ReversingAString.cs +++ b/src/Chapter03/Listing03.32.ReversingAString.cs @@ -20,7 +20,7 @@ public static void Main() temp = reverse.ToCharArray(); #endregion - #region INCLUDE + #region HIGHLIGHT // Reverse the array System.Array.Reverse(temp); #endregion From 402d4ee680c68b9e166467eb5336c4a050d3f418 Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Fri, 8 Apr 2022 19:00:36 -0700 Subject: [PATCH 3/6] Minor Update --- ...ltidimensionalArrayWithInconsistentSizeCausingAnError.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs b/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs index 8cf552374..b71012a46 100644 --- a/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs +++ b/src/Chapter03/Listing03.17.AMultidimensionalArrayWithInconsistentSizeCausingAnError.cs @@ -6,17 +6,19 @@ public static void Main() { #region INCLUDE // ERROR: Each dimension must be consistently sized - /* #region EXCLUDE + /* + #endregion int[,] cells = { {1, 0, 2, 0}, {1, 2, 0}, {1, 2}, {1} }; - #endregion + #region EXCLUDE */ #endregion + #endregion } } } From 9048b4b5dfbe34119e8ef0321996e9fe912607c0 Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Fri, 8 Apr 2022 19:05:47 -0700 Subject: [PATCH 4/6] Minor Updates --- src/Chapter03/Listing03.01.CheckingForNull.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chapter03/Listing03.01.CheckingForNull.cs b/src/Chapter03/Listing03.01.CheckingForNull.cs index c3a7e0e4a..2827bdf5f 100644 --- a/src/Chapter03/Listing03.01.CheckingForNull.cs +++ b/src/Chapter03/Listing03.01.CheckingForNull.cs @@ -3,7 +3,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_01 public class Program { #region INCLUDE - public static void Main() + public static void Main(string[] args) { int? number = null; #region EXCLUDE From 82d82096ebdd0f4ff075ec8698985554eaba7234 Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Mon, 11 Apr 2022 13:19:54 -0700 Subject: [PATCH 5/6] Updates from review --- .../Listing03.02.DereferencingAnUnassignedVariable.cs | 8 ++++---- src/Chapter03/Listing03.03.EnablingNullableProjectWide.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Chapter03/Listing03.02.DereferencingAnUnassignedVariable.cs b/src/Chapter03/Listing03.02.DereferencingAnUnassignedVariable.cs index ad6884044..6ed4ed036 100644 --- a/src/Chapter03/Listing03.02.DereferencingAnUnassignedVariable.cs +++ b/src/Chapter03/Listing03.02.DereferencingAnUnassignedVariable.cs @@ -2,15 +2,15 @@ //{ public class Program { - #region INCLUDE - #nullable enable - public static void Main() + #region INCLUDE + #nullable enable + public static void Main() { string? text; // ... // Compile Error: Use of unassigned local variable 'text' System.Console.WriteLine(text.Length); } + #endregion } - #endregion //} diff --git a/src/Chapter03/Listing03.03.EnablingNullableProjectWide.cs b/src/Chapter03/Listing03.03.EnablingNullableProjectWide.cs index f3e7a9f8a..ca3da2d8e 100644 --- a/src/Chapter03/Listing03.03.EnablingNullableProjectWide.cs +++ b/src/Chapter03/Listing03.03.EnablingNullableProjectWide.cs @@ -1,5 +1,5 @@ #region INCLUDE -< Project Sdk="Microsoft.NET.Sdk"> + Exe net6.0 From 33e83a4f61976ddf8d7bdaaa3b171c0f279a55f3 Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Mon, 11 Apr 2022 13:53:19 -0700 Subject: [PATCH 6/6] Minor Update --- src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs b/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs index e6217a25a..d8226a68e 100644 --- a/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs +++ b/src/Chapter03/Listing03.20.DeclaringAndAccessingAnArray.cs @@ -11,11 +11,11 @@ public static void Main() "Python", "Lisp", "JavaScript"}; // Retrieve fifth item in languages array (TypeScript) string language = languages[4]; - // Write �TypeScript� + // Write "TypeScript" System.Console.WriteLine(language); // Retrieve second item from the end (Python) language = languages[^3]; - // Write �Python� + // Write "Python" System.Console.WriteLine(language); #endregion }