Updated the source code for Essential C# 8.0; Added support for nullable.
MODIFY
src/Chapter05.Tests/Listing05.16.ReturningAReference.Tests.cs
src/Chapter05.Tests/Listing05.16.ReturningAReference.Tests.cs
@@ -8,9 +8,10 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_16.Tests
[TestMethod]
public void Main_InputInigoMontoya_WriteFullName()
{
- string view = "Black";
+ string view = @"image\[*\]=Red
+image\[*\]=Black";
- IntelliTect.TestTools.Console.ConsoleAssert.Expect(view,
+ IntelliTect.TestTools.Console.ConsoleAssert.ExpectLike(view,
() =>
{
Program.Main();

MODIFY
src/Chapter05/Listing05.12.PassingCommandLineArgumentsToMain.cs
src/Chapter05/Listing05.12.PassingCommandLineArgumentsToMain.cs
@@ -10,8 +10,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_12
public static int Main(string[] args)
{
int result;
- string targetFileName;
- string url;
switch(args.Length)
{
@@ -20,32 +18,15 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_12
Console.WriteLine(
"ERROR: You must specify the "
+ "URL and the file name");
- targetFileName = null;
- url = null;
- break;
- case 2:
- url = args[0];
- targetFileName = args[1];
- break;
- }
-
- if(targetFileName != null && url != null)
- {
- using (HttpClient httpClient = new HttpClient())
- using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url))
- using (HttpResponseMessage message = httpClient.SendAsync(request).Result)
- using (Stream contentStream = message.Content.ReadAsStreamAsync().Result)
- using (FileStream fileStream = new FileStream(targetFileName, FileMode.Create, FileAccess.Write, FileShare.None))
- {
- contentStream.CopyToAsync(fileStream);
- }
- result = 0;
- }
- else
- {
Console.WriteLine(
"Usage: Downloader.exe <URL> <TargetFileName>");
result = 1;
+ break;
+ case 2:
+ WebClient webClient = new WebClient();
+ webClient.DownloadFile(args[0], args[1]);
+ result = 0;
+ break;
}
return result;

MODIFY
src/Chapter05/Listing05.16.ReturningAReference.cs
src/Chapter05/Listing05.16.ReturningAReference.cs
@@ -5,7 +5,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_16
public class Program
{
// Returning a reference
- static public ref byte FindFirstRedEyePixel(byte[] image)
+ public static ref byte FindFirstRedEyePixel(byte[] image)
{
// Do fancy image detection perhaps with machine learning
for (int counter = 0; counter < image.Length; counter++)
@@ -19,20 +19,22 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_16
}
public static void Main()
{
- byte[] image = null;
+ byte[] image = new byte[254];
// Load image
- image = new byte[42];
- for (int i = 0; i < image.Length; i++)
- {
- image[i] = (byte)ConsoleColor.Black;
- }
- image[(new Random()).Next(0, image.Length - 1)] = (byte)ConsoleColor.Red;
+ int index = new Random().Next(0, image.Length - 1);
+ image[index] =
+ (byte)ConsoleColor.Red;
+ System.Console.WriteLine(
+ $"image[{index}]={(ConsoleColor)image[index]}");
+ // ...
// Obtain a reference to the first red pixel
ref byte redPixel = ref FindFirstRedEyePixel(image);
// Update it to be Black
redPixel = (byte)ConsoleColor.Black;
- System.Console.WriteLine((ConsoleColor)image[redPixel]);
+ System.Console.WriteLine(
+ $"image[{index}]={(ConsoleColor)image[redPixel]}");
}
+
}
}

MODIFY
src/Chapter05/Listing05.18.CountingLinesGivenADirectory.cs
src/Chapter05/Listing05.18.CountingLinesGivenADirectory.cs
+#nullable enable
+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_18
{
using System.IO;
@@ -42,7 +44,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_18
private static int CountLines(string file)
{
- string line;
+ string? line;
int lineCount = 0;
FileStream stream =
new FileStream(file, FileMode.Open);

MODIFY
src/Chapter05/Listing05.19.CountingLinesUsingOverloading.cs
src/Chapter05/Listing05.19.CountingLinesUsingOverloading.cs
@@ -58,12 +58,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_19
private static int CountLines(string file)
{
int lineCount = 0;
- string line;
+ string? line;
FileStream stream =
new FileStream(file, FileMode.Open);
StreamReader reader = new StreamReader(stream);
line = reader.ReadLine();
- while(line != null)
+ while(line is object)
{
if(line.Trim() != "")
{

MODIFY
src/Chapter05/Listing05.20.MethodsWithOptionalParameters.cs
src/Chapter05/Listing05.20.MethodsWithOptionalParameters.cs
@@ -59,12 +59,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_20
private static int CountLines(string file)
{
int lineCount = 0;
- string line;
+ string? line;
FileStream stream =
new FileStream(file, FileMode.Open);
StreamReader reader = new StreamReader(stream);
line = reader.ReadLine();
- while (line != null)
+ while (line is object)
{
if (line.Trim() != "")
{

MODIFY
src/Chapter05/Listing05.21.SpecifyingParametersByName.cs
src/Chapter05/Listing05.21.SpecifyingParametersByName.cs
@@ -12,8 +12,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_21
public static void DisplayGreeting(
string firstName,
- string middleName = default(string),
- string lastName = default(string))
+ string? middleName = null,
+ string? lastName = null
+ )
{
// ...
}

Updated the source code for Essential C# 8.0; Added support for nullable.