Add regions in chapter 5
MODIFY
src/Chapter05/Listing05.19.CountingLinesUsingOverloading.cs
src/Chapter05/Listing05.19.CountingLinesUsingOverloading.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_19
{
+ #region INCLUDE
using System.IO;
public static class LineCounter
@@ -10,33 +11,45 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_19
if(args.Length > 1)
{
+ #region HIGHLIGHT
totalLineCount =
DirectoryCountLines(args[0], args[1]);
+ #endregion
}
else if(args.Length > 0)
{
+ #region HIGHLIGHT
totalLineCount = DirectoryCountLines(args[0]);
+ #endregion
}
else
{
+ #region HIGHLIGHT
totalLineCount = DirectoryCountLines();
+ #endregion
}
System.Console.WriteLine(totalLineCount);
}
+ #region HIGHLIGHT
static int DirectoryCountLines()
+ #endregion
{
return DirectoryCountLines(
Directory.GetCurrentDirectory());
}
+ #region HIGHLIGHT
static int DirectoryCountLines(string directory)
+ #endregion
{
return DirectoryCountLines(directory, "*.cs");
}
+ #region HIGHLIGHT
static int DirectoryCountLines(
+ #endregion
string directory, string extension)
{
int lineCount = 0;
@@ -76,4 +89,5 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_19
return lineCount;
}
}
+ #endregion
}
\ No newline at end of file

MODIFY
src/Chapter05/Listing05.20.MethodsWithOptionalParameters.cs
src/Chapter05/Listing05.20.MethodsWithOptionalParameters.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_20
{
+ #region INCLUDE
using System.IO;
public static class LineCounter
@@ -15,7 +16,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_20
}
else if(args.Length > 0)
{
+ #region HIGHLIGHT
totalLineCount = DirectoryCountLines(args[0]);
+ #endregion
}
else
{
@@ -27,18 +30,22 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_20
static int DirectoryCountLines()
{
+ #region EXCLUDE
return 0;
-
- // ...
+ #endregion
}
+ #region HIGHLIGHT
/*
static int DirectoryCountLines(string directory)
{ ... }
*/
+ #endregion
+ #region HIGHLIGHT
static int DirectoryCountLines(
string directory, string extension = "*.cs")
+ #endregion
{
int lineCount = 0;
foreach(string file in
@@ -58,6 +65,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_20
private static int CountLines(string file)
{
+ #region EXCLUDE
int lineCount = 0;
string? line;
FileStream stream =
@@ -75,6 +83,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_20
reader.Dispose(); // Automatically closes the stream
return lineCount;
+ #endregion
}
}
+ #endregion
}

MODIFY
src/Chapter05/Listing05.21.SpecifyingParametersByName.cs
src/Chapter05/Listing05.21.SpecifyingParametersByName.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_21
{
+ #region INCLUDE
public class Program
{
public static void Main()
{
+ #region HIGHLIGHT
DisplayGreeting(
firstName: "Inigo", lastName: "Montoya");
+ #endregion
}
public static void DisplayGreeting(
@@ -17,4 +20,5 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_21
// ...
}
}
+ #endregion
}

MODIFY
src/Chapter05/Listing05.22.ConvertingAStringToAnInt.cs
src/Chapter05/Listing05.22.ConvertingAStringToAnInt.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_22
{
+ #region INCLUDE
using System;
public class ExceptionHandling
@@ -13,17 +14,18 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_22
Console.WriteLine("Hey you!");
Console.Write("Enter your first name: ");
- // TODO: Update listing in Manuscript
firstName = Console.ReadLine();
+ #region HIGHLIGHT
Console.Write("Enter your age: ");
- // TODO: Update listing in Manuscript
// Assume not null for clarity
ageText = Console.ReadLine()!;
age = int.Parse(ageText);
Console.WriteLine(
$"Hi { firstName }! You are { age * 12 } months old.");
+ #endregion
}
}
+ #endregion
}

MODIFY
src/Chapter05/Listing05.23.CatchingAnException.cs
src/Chapter05/Listing05.23.CatchingAnException.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_23
{
+ #region INCLUDE
using System;
public class ExceptionHandling
@@ -12,11 +13,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_23
int result = 0;
Console.Write("Enter your first name: ");
- // TODO: Update listing in Manuscript
firstName = Console.ReadLine();
Console.Write("Enter your age: ");
- // TODO: Update listing in Manuscript
// Assume not null for clarity
ageText = Console.ReadLine()!;
@@ -46,4 +45,5 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_23
return result;
}
}
+ #endregion
}

MODIFY
src/Chapter05/Listing05.24.FinallyWithoutCatch.cs
src/Chapter05/Listing05.24.FinallyWithoutCatch.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_24
{
+ #region INCLUDE
using System;
public class ExceptionHandling
@@ -12,11 +13,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_24
int result = 0;
Console.Write("Enter your first name: ");
- // TODO: Update listing in Manuscript
firstName = Console.ReadLine();
Console.Write("Enter your age: ");
- // TODO: Update listing in Manuscript
// Assume not null for clarity
ageText = Console.ReadLine()!;
@@ -34,4 +33,5 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_24
return result;
}
}
+ #endregion
}
\ No newline at end of file

MODIFY
src/Chapter05/Listing05.25.GeneralCatchBlocks.cs
src/Chapter05/Listing05.25.GeneralCatchBlocks.cs
-#pragma warning disable CS1058 // A previous catch clause already catches all exceptions
#pragma warning disable CS0168 // Variable is declared but never used
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_25
{
- // TODO: Update listing in Manuscript
+ #region INCLUDE
+ // A previous catch clause already catches all exceptions
+ #pragma warning disable CS1058
+ #region EXCLUDE
using System;
public class ExceptionHandling
@@ -15,14 +17,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_25
int result = 0;
Console.Write("Enter your first name: ");
- // TODO: Update listing in Manuscript
firstName = Console.ReadLine();
Console.Write("Enter your age: ");
- // TODO: Update listing in Manuscript
// Assume not null for clarity
ageText = Console.ReadLine()!;
+ #endregion
try
{
age = int.Parse(ageText);
@@ -50,7 +51,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_25
{
Console.WriteLine($"Goodbye { firstName }");
}
-
+ #endregion
return result;
}
}

MODIFY
src/Chapter05/Listing05.26.ThrowingAnException.cs
src/Chapter05/Listing05.26.ThrowingAnException.cs
-#pragma warning disable CS1058 // A previous catch clause already catches all exceptions
+#pragma warning disable CS1058
#pragma warning disable CS0168 // Variable is declared but never used
#pragma warning disable CS0162 // Unreachable code detected
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_26
{
+ #region INCLUDE
+ // A previous catch clause already catches all exceptions
using System;
public class ThrowingExceptions
@@ -13,7 +15,9 @@ public class ThrowingExceptions
{
Console.WriteLine("Begin executing");
Console.WriteLine("Throw exception");
+ #region HIGHLIGHT
throw new Exception("Arbitrary exception");
+ #endregion
Console.WriteLine("End executing");
}
catch(FormatException exception)
@@ -35,4 +39,5 @@ public class ThrowingExceptions
"Shutting down...");
}
}
+ #endregion
}

MODIFY
src/Chapter05/Listing05.27.RethrowingAnException.cs
src/Chapter05/Listing05.27.RethrowingAnException.cs
@@ -21,6 +21,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_27
Console.WriteLine(
"A FormateException was thrown");
}
+ #region INCLUDE
+ // ...
catch (Exception exception)
{
Console.WriteLine(
@@ -29,6 +31,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_27
throw;
}
+ // ...
+ #endregion
catch
{
Console.WriteLine("Unexpected error!");

MODIFY
src/Chapter05/Listing05.28.TryCatch.cs
src/Chapter05/Listing05.28.TryCatch.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_28
{
- // TODO: Update listing in Manuscript
using System;
public class LeveragingTryParse
@@ -11,29 +10,24 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_28
string ageText;
Console.Write("Enter your first name: ");
- // TODO: Update listing in Manuscript
firstName = Console.ReadLine();
Console.Write("Enter your age: ");
- // TODO: Update listing in Manuscript
// Assume not null for clarity
ageText = Console.ReadLine()!;
-
-#if !PRECSHARP7
+ #region INCLUDE
if (int.TryParse(ageText, out int age))
-#else
- int age;
- if(int.TryParse(ageText, out age))
-#endif // !PRECSHARP7
{
Console.WriteLine(
- $"Hi { firstName }! You are { age * 12 } months old.");
+ $"Hi { firstName }! " +
+ $"You are { age * 12 } months old.");
}
else
{
Console.WriteLine(
$"The age entered ,{ ageText }, is not valid.");
}
+ #endregion
}
}
}

Add regions in chapter 5